计算最高评分值的数量
count number of highest ratings value
我有一个 table FixtureStats
Id FixtureId PlayerID Rating other Attributes
1 1 1 8.5
2 1 2 6.8
3 1 3 9.1 << man of the match(Motm)
.....
100 4 1 5.3
101 4 2 7.6 << Motm
102 4 3 4.5
我想要 select 玩家列表和达到的 Motm 数量。
例如
PlayerId Motm
1 0
2 1
3 1
谢谢大家
您可以为此使用 ROW_NUMBER()
函数:
;with cte AS (SELECT *
,CASE WHEN ROW_NUMBER() OVER(PARTITION BY FixtureID ORDER BY Rating DESC) = 1 THEN 1 END AS Motm
FROM FixturStats
)
SELECT PlayerID
,SUM(Motm) AS Motm
FROM cte
GROUP BY PlayerID
因为你只关心 Motm
我把它包装在 CASE
所以它是 1
或 NULL
,然后可以简单地聚合。如果您想查看某人排名第二或第三的频率,您可以删除 CASE
并以不同方式汇总。
ROW_NUMBER()
为由 PARTITION BY
确定的组中的每一行创建一个数字,并根据 ORDER BY
进行编号,在这种情况下,您需要顶部 rating
来自每个 FixtureID
,所以我们使用它们。
您可以使用 ROW_NUMBER()
:
WITH CTE AS(
SELECT
*,
rn = ROW_NUMBER() OVER(PARTITION BY FixtureId ORDER BY Rating DESC)
FROM FixtureStats
)
SELECT
p.PlayerID,
Motm = COUNT(c.PlayerID)
FROM (
SELECT DISTINCT PlayerID FROM FixtureStats
)p
LEFT JOIN CTE c
ON c.PlayerID = p.PlayerID
AND c.rn = 1
GROUP BY p.PlayerID
我有一个 table FixtureStats
Id FixtureId PlayerID Rating other Attributes
1 1 1 8.5
2 1 2 6.8
3 1 3 9.1 << man of the match(Motm)
.....
100 4 1 5.3
101 4 2 7.6 << Motm
102 4 3 4.5
我想要 select 玩家列表和达到的 Motm 数量。
例如
PlayerId Motm
1 0
2 1
3 1
谢谢大家
您可以为此使用 ROW_NUMBER()
函数:
;with cte AS (SELECT *
,CASE WHEN ROW_NUMBER() OVER(PARTITION BY FixtureID ORDER BY Rating DESC) = 1 THEN 1 END AS Motm
FROM FixturStats
)
SELECT PlayerID
,SUM(Motm) AS Motm
FROM cte
GROUP BY PlayerID
因为你只关心 Motm
我把它包装在 CASE
所以它是 1
或 NULL
,然后可以简单地聚合。如果您想查看某人排名第二或第三的频率,您可以删除 CASE
并以不同方式汇总。
ROW_NUMBER()
为由 PARTITION BY
确定的组中的每一行创建一个数字,并根据 ORDER BY
进行编号,在这种情况下,您需要顶部 rating
来自每个 FixtureID
,所以我们使用它们。
您可以使用 ROW_NUMBER()
:
WITH CTE AS(
SELECT
*,
rn = ROW_NUMBER() OVER(PARTITION BY FixtureId ORDER BY Rating DESC)
FROM FixtureStats
)
SELECT
p.PlayerID,
Motm = COUNT(c.PlayerID)
FROM (
SELECT DISTINCT PlayerID FROM FixtureStats
)p
LEFT JOIN CTE c
ON c.PlayerID = p.PlayerID
AND c.rn = 1
GROUP BY p.PlayerID