SQL 服务器数据冗余
SQL Server Data redundancy
我正在尝试找出 F1 车队遵循的进站策略,以及哪种策略平均产生更好的获胜结果。然而,每次我尝试执行查询时,我只会得到冗余数据。
数据集示例:
驱动表
DriverId
DriverRef
DriverNumber
1
Lewis
22
2
Alonso
14
3
Max
1
比赛结果
ResultID
RaceID
DriverID
Number
Position
Laps
1
12
1
22
1
53
1
12
2
14
6
53
1
12
3
1
2
53
2
13
1
22
2
57
2
13
2
14
6
57
2
13
3
1
1
57
种族
RaceID
year
CircuitID
Name
12
2009
1
Monza
13
2013
2
Bahrain Sakhir
进站
RaceID
DriverID
Stop
Lap
12
1
1
17
12
1
2
34
12
2
1
14
12
2
2
42
12
3
1
20
12
3
2
37
13
1
1
14
13
1
2
32
13
2
1
12
13
2
2
34
13
3
1
20
13
3
2
42
我想要的结果 table 看起来与此类似。
策略选择
DriverRef
RaceID
CircuitID
Name
Stop
Lap
Lewis
12
1
Monza
1
17
Lewis
12
1
Monza
2
34
Max
13
2
Bahrain Sakhir
1
20
Max
13
2
Bahrain Sakhir
2
42
这里的目标是找出获胜的车手在某条赛道上使用了什么进站策略。
SELECT ra.year, ra.name, d.properdriverref, (SELECT DISTINCT re.number FROM results WHERE re.position = 1), p.stop, p.lap, re.position
FROM pit_stops p
JOIN results re ON re.raceId = p.raceId
JOIN races ra ON p.raceId = ra.raceId
JOIN DriversXL d ON p.driverId = d.driverId
WHERE ra.year >= 2018 AND re.position = 1
是不是我用的代码和数据集返回了一个
DriverRef
RaceID
CircuitID
Name
Stop
Lap
Lewis
12
1
Monza
1
17
Lewis
12
1
Monza
2
34
Max
12
1
Monza
1
17
Max
12
1
Monza
2
34
Alonso
12
1
Monza
1
17
Alonso
12
1
Monza
2
34
Max
13
2
Bahrain Sakhir
1
20
Max
13
2
Bahrain Sakhir
2
42
Lewis
13
2
Bahrain Sakhir
1
20
Lewis
13
2
Bahrain Sakhir
2
42
Alonso
13
2
Bahrain Sakhir
1
20
Alonso
13
2
Bahrain Sakhir
2
42
您在 pit_stop
和 results
之间缺少一个额外的连接列。
It's unclear the point of that subquery, so I have removed it
SELECT
d.driverref,
r.raceId,
r.circuitId,
ra.year,
ra.name,
p.stop,
p.lap
FROM pit_stops p
JOIN results re ON re.raceId = p.raceId AND re.DriverID = p.DriverID
JOIN races ra ON p.raceId = ra.raceId
JOIN DriversXL d ON p.driverId = d.driverId
WHERE re.position = 1;
我正在尝试找出 F1 车队遵循的进站策略,以及哪种策略平均产生更好的获胜结果。然而,每次我尝试执行查询时,我只会得到冗余数据。
数据集示例:
驱动表
DriverId | DriverRef | DriverNumber |
---|---|---|
1 | Lewis | 22 |
2 | Alonso | 14 |
3 | Max | 1 |
比赛结果
ResultID | RaceID | DriverID | Number | Position | Laps |
---|---|---|---|---|---|
1 | 12 | 1 | 22 | 1 | 53 |
1 | 12 | 2 | 14 | 6 | 53 |
1 | 12 | 3 | 1 | 2 | 53 |
2 | 13 | 1 | 22 | 2 | 57 |
2 | 13 | 2 | 14 | 6 | 57 |
2 | 13 | 3 | 1 | 1 | 57 |
种族
RaceID | year | CircuitID | Name |
---|---|---|---|
12 | 2009 | 1 | Monza |
13 | 2013 | 2 | Bahrain Sakhir |
进站
RaceID | DriverID | Stop | Lap |
---|---|---|---|
12 | 1 | 1 | 17 |
12 | 1 | 2 | 34 |
12 | 2 | 1 | 14 |
12 | 2 | 2 | 42 |
12 | 3 | 1 | 20 |
12 | 3 | 2 | 37 |
13 | 1 | 1 | 14 |
13 | 1 | 2 | 32 |
13 | 2 | 1 | 12 |
13 | 2 | 2 | 34 |
13 | 3 | 1 | 20 |
13 | 3 | 2 | 42 |
我想要的结果 table 看起来与此类似。
策略选择
DriverRef | RaceID | CircuitID | Name | Stop | Lap |
---|---|---|---|---|---|
Lewis | 12 | 1 | Monza | 1 | 17 |
Lewis | 12 | 1 | Monza | 2 | 34 |
Max | 13 | 2 | Bahrain Sakhir | 1 | 20 |
Max | 13 | 2 | Bahrain Sakhir | 2 | 42 |
这里的目标是找出获胜的车手在某条赛道上使用了什么进站策略。
SELECT ra.year, ra.name, d.properdriverref, (SELECT DISTINCT re.number FROM results WHERE re.position = 1), p.stop, p.lap, re.position
FROM pit_stops p
JOIN results re ON re.raceId = p.raceId
JOIN races ra ON p.raceId = ra.raceId
JOIN DriversXL d ON p.driverId = d.driverId
WHERE ra.year >= 2018 AND re.position = 1
是不是我用的代码和数据集返回了一个
DriverRef | RaceID | CircuitID | Name | Stop | Lap |
---|---|---|---|---|---|
Lewis | 12 | 1 | Monza | 1 | 17 |
Lewis | 12 | 1 | Monza | 2 | 34 |
Max | 12 | 1 | Monza | 1 | 17 |
Max | 12 | 1 | Monza | 2 | 34 |
Alonso | 12 | 1 | Monza | 1 | 17 |
Alonso | 12 | 1 | Monza | 2 | 34 |
Max | 13 | 2 | Bahrain Sakhir | 1 | 20 |
Max | 13 | 2 | Bahrain Sakhir | 2 | 42 |
Lewis | 13 | 2 | Bahrain Sakhir | 1 | 20 |
Lewis | 13 | 2 | Bahrain Sakhir | 2 | 42 |
Alonso | 13 | 2 | Bahrain Sakhir | 1 | 20 |
Alonso | 13 | 2 | Bahrain Sakhir | 2 | 42 |
您在 pit_stop
和 results
之间缺少一个额外的连接列。
It's unclear the point of that subquery, so I have removed it
SELECT
d.driverref,
r.raceId,
r.circuitId,
ra.year,
ra.name,
p.stop,
p.lap
FROM pit_stops p
JOIN results re ON re.raceId = p.raceId AND re.DriverID = p.DriverID
JOIN races ra ON p.raceId = ra.raceId
JOIN DriversXL d ON p.driverId = d.driverId
WHERE re.position = 1;