我正在尝试使用SQL查找播放器合成的频率或数量。这是我的数据集
event key team_id player_id
12235384031441051453625936 1 27
12235384031441051453625936 1 11
12235384031441051453625936 1 42
12235384031441051453625936 1 15
12235384031441051453625936 1 6
12235384031441051453625936 2 30
12235384031441051453625936 2 35
12235384031441051453625936 2 9
12235384031441051453625936 2 6
12235384031441051453625936 2 28
12235384031441051453626082 1 14
12235384031441051453626082 1 28
12235384031441051453626082 1 27
12235384031441051453626082 1 9
12235384031441051453626082 1 6
12235384031441051453626082 2 11
12235384031441051453626082 2 27
12235384031441051453626082 2 42
12235384031441051453626082 2 15
12235384031441051453626082 2 6
因此,对于每个赛事,球队1和球队2都有球员,球员代表球员。我有大约500个赛事,很多这些球员将出现在多个赛事中,但并不总是与同一个球员一起出现。每支球队总有5名球员,活动键代表一场比赛。所以我基本上有500个以上的数据集。
我正在努力寻找最常见的团队组合。例如,使用上面的数据集,我看到玩家27,11,42,15,6事件以936结尾,事件结束于082.因此,根据THIS数据集,该团队组合的COUNT为2.
我正在尝试获得一个输出,该输出计算一组玩家在同一个团队中出现的次数,并且我正在努力想出一个连贯的声明来做到这一点。
答案 0 :(得分:5)
我认为获得最常见团队组合的最简单方法是使用listagg()
将成员聚集在一起:
select players, count(*) as numevents
from (select listagg(player_id, ',') within group (order by player_id) as players
from t
group by eventkey, team_id
) p
group by players
order by count(*) desc;
答案 1 :(得分:0)
StackOverflow中我喜欢的一件事就是从别人的问题中学到一些东西。因为我喜欢这个问题和Gordon Linoff的答案(两者都是+1),我根据他的答案发布了SQL Server版本,因为我试图重现它,但MSSQL上没有listagg。谁知道它是否对有相同问题但DBMS不同的人有帮助。
表(没有大型事件ID)
DECLARE @t1 TABLE (event_key INT, team_id INT, player_id INT)
INSERT INTO @t1 VALUES (1,1,27);
INSERT INTO @t1 VALUES (1,1,11);
INSERT INTO @t1 VALUES (1,1,42);
INSERT INTO @t1 VALUES (1,1,15);
INSERT INTO @t1 VALUES (1,1,6);
INSERT INTO @t1 VALUES (1,2,30);
INSERT INTO @t1 VALUES (1,2,35);
INSERT INTO @t1 VALUES (1,2,9);
INSERT INTO @t1 VALUES (1,2,6);
INSERT INTO @t1 VALUES (1,2,28);
INSERT INTO @t1 VALUES (2,1,14);
INSERT INTO @t1 VALUES (2,1,28);
INSERT INTO @t1 VALUES (2,1,27);
INSERT INTO @t1 VALUES (2,1,9);
INSERT INTO @t1 VALUES (2,1,6);
INSERT INTO @t1 VALUES (2,2,11);
INSERT INTO @t1 VALUES (2,2,27);
INSERT INTO @t1 VALUES (2,2,42);
INSERT INTO @t1 VALUES (2,2,15);
INSERT INTO @t1 VALUES (2,2,6);
<强>代码强>
select players, count(*) as numevents
from (select stuff((
SELECT ', ' + CAST(player_id as nvarchar)
FROM @t1 as t1
WHERE t1.event_key=t2.event_key
AND t1.team_id=t2.team_id
ORDER BY player_id
FOR XML PATH('')
), 1, 2, '') as players
from @t1 as t2
group by event_key, team_id
) p
group by players
order by count(*) desc;