首页 > 解决方案 > 统计玩家的sql查询

问题描述

在特定国家/地区的领先者的 500 场比赛中得分的球员计数。例如,如果印度 A 得分 1500 分,那么有多少球员得分超过 1000 分。

我已经运行了这段代码,但这并没有给我想要的结果。

select COUNT( [Player ] )as no_of_players,country from [dbo].[Sheet1$]
where runs >=
All (select max(runs)-500 from [dbo].[Sheet1$] group by country )
group by country

下面是我创建的数据集的图像

标签: sql-servertsql

解决方案


您可以为此使用窗口函数

SELECT
  COUNT(*) AS no_of_players,
  country
FROM (
    SELECT *,
      MAX(runs) OVER (PARTITION BY country) AS MaxRuns
    FROM [dbo].[Sheet1$] s1
) s1
WHERE runs >= MaxRuns - 500;

请注意,与或COUNT(nonNullValue)相同COUNT(*)COUNT(1)


推荐阅读