最大/最小/计数等没有给出好的价值

时间:2016-05-26 07:39:19

标签: sql sql-server tsql

我有来自database的f1 ergast.com。我试着为每个种族中的每个驱动程序获得最佳laptimepitstops个数,所以我写了这样的SQL查询:

SELECT results.driverId, results.constructorId, races.circuitId, races."year"-1949,
       results.statusId, results.positionOrder, results.grid, results.points, 
       results.laps, ISNULL(max(pitstops.stop), 0), min(laptimes.milliseconds), 
       results.fastestLapTime, drivers.forename, drivers.surname, circuits.name
FROM results 
INNER JOIN races 
   ON (results.raceId = races.raceId)
LEFT OUTER JOIN pitstops 
   ON (races.raceId = pitstops.raceId)
LEFT OUTER JOIN laptimes 
   ON (laptimes.raceId = races.raceId)
INNER JOIN drivers 
   ON (drivers.driverId = results.driverId)
INNER JOIN circuits 
   ON (circuits.circuitId = races.circuitId)
WHERE ((races."year"=2011) OR (races."year"=2012))
  AND ((races.circuitId = 1) OR (races.circuitId = 2))
GROUP BY results.driverId, constructorId, races.circuitId, races."year"-1949,
         statusId, positionOrder, grid, points, laps, fastestLapTime, forename,
         surname, circuits.name

不幸的是,对于给定比赛中的每个车手,这给了我最好的laptimepitstops的数量。我该如何解决?我应该向group by添加内容吗?

3 个答案:

答案 0 :(得分:2)

您只在raceId上加入进站,从而获得每场比赛的最大进站数。但是你说“给定比赛中每个车手的进站次数”。这表明pitstop应该与驱动程序相关,例如:

LEFT OUTER JOIN pitstops 
   ON (results.raceId = pitstops.raceId and results.driverId = pitstops.driverId)

laptimes也是如此。

(您的查询中可能会有更多错误。如果您需要更多帮助,请使用表名限定所有列,因此我们会看到哪些列位于哪个表中。更好的是表定义。)

答案 1 :(得分:1)

添加已经给出的答案:您可以通过在子查询中获取聚合来简化查询(在FROM子句或SELECT子句中)。 E.g:

select 
  results.driverid,
  results.constructorid,
  races.circuitid,
  races."year" - 1949,
  results.statusid,
  results.positionorder,
  results.grid,
  results.points,
  results.laps,
  (
    select coalesce(max(stop), 0)
    from pitstops
    where pitstops.raceid = results.raceid
    and pitstops.driverid = results.driverid
  ) as max_pitstops,
  (
    select coalesce(min(milliseconds), 0)
    from laptimes
    where laptimes.raceid = results.raceid
    and laptimes.driverid = results.driverid
  ) as min_laptimes,
  results.fastestlaptime,
  drivers.forename,
  drivers.surname,
  circuits.name
from results
join drivers on drivers.driverid = results.driverid
join races on races.raceid = results.raceid
join circuits on circuits.circuitid = races.circuitid
where races."year" in (2011, 2012)
and races.circuitid in (1, 2);

select 
  results.driverid,
  results.constructorid,
  races.circuitid,
  races."year" - 1949,
  results.statusid,
  results.positionorder,
  results.grid,
  results.points,
  results.laps,
  coalesce(pits.max_stop, 0) as max_pitstops,
  coalesce(laps.min_milliseconds, 0) as min_laptimes,
  results.fastestlaptime,
  drivers.forename,
  drivers.surname,
  circuits.name
from results
join drivers on drivers.driverid = results.driverid
join races on races.raceid = results.raceid
join circuits on circuits.circuitid = races.circuitid
left join
(
  select raceid, driverid, max(stop) as max_stop
  from pitstops
  group by raceid, driverid
) pits on pits.raceid = results.raceid and pits.driverid = results.driverid
left join
(
  select raceid, driverid, min(milliseconds) as min_milliseconds
  from laptimes
  group by raceid, driverid
) laps on laps.raceid = results.raceid and laps.driverid = results.driverid
where races."year" in (2011, 2012)
and races.circuitid in (1, 2);

答案 2 :(得分:1)

这是又一个“答案”: - )

您正在使用技术ID(raceId,driverId等),您很惊讶必须加入多个专栏。对于基于技术ID的数据库,这确实有点不典型。那么让我们来看看为什么......

应该有一张表格列出了每场比赛的车手。我们称之为participation

participation(participationid, raceid, driverid)

使用此类表格,您的其他表格将不再包含raceiddriverid的组合,但只包含participationid

pitstops(pitstopid, participationid, stop, ...)

laptimes(laptimeid, participationid, milliseconds, ...)

results(resultid, participationid, points, ...)

你只会加入那一栏:

LEFT OUTER JOIN pitstops 
  ON pitstops.participationid = results.participationid

但是,我认为每raceiddriverid只能有一个结果记录?然后你的参与表真的是结果表(只有更糟的名字:-)。因此,您应该在表格和其他表格中对其进行重新分析:

pitstops(pitstopid, resultid, stop, ...)

laptimes(laptimeid, resultid, milliseconds, ...)

你只会加入那一栏:

LEFT OUTER JOIN pitstops 
  ON pitstops.resultid = results.resultid