如何获得按匹配列分组的平均行数?
How to get an average number of rows grouped by matched column?
我有一个赛车运动数据库,需要创建一个查询来查找每年每场比赛(姓名)的平均进站总次数。
每一行都是一个进站,我计算了每年有多少次进站。
我当前的查询是:
SELECT
pitstopRaceName AS raceName,
(SELECT AVG(COUNT(pitstopRaceDate))) AS totalPitstop
FROM MoSpo_PitStop
GROUP BY pitstopRaceName ,pitstopRaceDate
ORDER BY pitstopRaceName
这给出了结果:
raceName
totalPitstop
British Grand Prix
1.0000
British Grand Prix
6.0000
British GT Championship
'2.0000
German Grand Prix
5.0000
German Grand Prix
1.0000
German Grand Prix
4.0000
Italian Grand Prix
1.0000
Italian Grand Prix
5.0000
每一行都是它自己的年份,我只是没有将它们包含在查询中
但我需要的是所有比赛名称的平均总进站次数,例如英国大奖赛平均 6 和 1 得到 3.5,意大利大奖赛平均 1 和 5 得到 3 等等。
我不知道如何完成这项工作,希望得到任何帮助。
But what i need is all race names to average their total pitstops, for example British Grand Prix would average 6 and 1 to get 3.5, Italian Grand Prix to average 1 and 5 to get 3 and so on
我怀疑您想要两级聚合。首先按年份和比赛计算进站次数,然后仅按比赛计算每场比赛的年平均值:
select race_name, avg(cnt_pitstops) as avg_pitsops_per_year
from (
select year(pitstopracedate) as race_year,
pitstopracename as race_name,
count(*) as cnt_pitstops
from mospo_pitstop
group by race_year, race_name
) t
如果我没理解错的话,你可以算进站次数除以年数:
select pitstopRaceName,
count(*) / count(distinct pitstopRaceDate)
from MoSpo_PitStop ps
group by pitstopRaceName;
如果pitstopRaceDate
没有定义种族,你可以只使用年份:
select pitstopRaceName,
count(*) / count(distinct year(pitstopRaceDate))
from MoSpo_PitStop ps
group by pitstopRaceName;
我有一个赛车运动数据库,需要创建一个查询来查找每年每场比赛(姓名)的平均进站总次数。 每一行都是一个进站,我计算了每年有多少次进站。
我当前的查询是:
SELECT
pitstopRaceName AS raceName,
(SELECT AVG(COUNT(pitstopRaceDate))) AS totalPitstop
FROM MoSpo_PitStop
GROUP BY pitstopRaceName ,pitstopRaceDate
ORDER BY pitstopRaceName
这给出了结果:
raceName | totalPitstop |
---|---|
British Grand Prix | 1.0000 |
British Grand Prix | 6.0000 |
British GT Championship | '2.0000 |
German Grand Prix | 5.0000 |
German Grand Prix | 1.0000 |
German Grand Prix | 4.0000 |
Italian Grand Prix | 1.0000 |
Italian Grand Prix | 5.0000 |
每一行都是它自己的年份,我只是没有将它们包含在查询中
但我需要的是所有比赛名称的平均总进站次数,例如英国大奖赛平均 6 和 1 得到 3.5,意大利大奖赛平均 1 和 5 得到 3 等等。 我不知道如何完成这项工作,希望得到任何帮助。
But what i need is all race names to average their total pitstops, for example British Grand Prix would average 6 and 1 to get 3.5, Italian Grand Prix to average 1 and 5 to get 3 and so on
我怀疑您想要两级聚合。首先按年份和比赛计算进站次数,然后仅按比赛计算每场比赛的年平均值:
select race_name, avg(cnt_pitstops) as avg_pitsops_per_year
from (
select year(pitstopracedate) as race_year,
pitstopracename as race_name,
count(*) as cnt_pitstops
from mospo_pitstop
group by race_year, race_name
) t
如果我没理解错的话,你可以算进站次数除以年数:
select pitstopRaceName,
count(*) / count(distinct pitstopRaceDate)
from MoSpo_PitStop ps
group by pitstopRaceName;
如果pitstopRaceDate
没有定义种族,你可以只使用年份:
select pitstopRaceName,
count(*) / count(distinct year(pitstopRaceDate))
from MoSpo_PitStop ps
group by pitstopRaceName;