我有sql结果:
+----------+-----+--------+
|rewardID |count|playerID|
+----------+-----+--------+
|BurgerKing|5 |1 |
+----------+-----+--------+
|BurgerKing|4 |2 |
+----------+-----+--------+
|KFC |1 |1 |
+----------+-----+--------+
|KFC |5 |2 |
+----------+-----+--------+
我怎样才能使它像
一样+----------+-----+--------+
|rewardID |count|playerID|
+----------+-----+--------+
|BurgerKing|5 |1 |
+----------+-----+--------+
|KFC |5 |2 |
+----------+-----+--------+
我的意思是选择每个奖励行获得最多奖励的玩家
答案 0 :(得分:0)
尝试以下
select Y.rewardId,Y.count,[playerId] from YourTable Y
join (select [rewardId],MAX([count]) as [count] from YourTable group by [rewardId]) as Y1
on Y.rewardId=Y1.rewardId and Y.[count]=Y1.[count]
尝试使用下表。
CREATE TABLE [dbo].[yourtable]( [rewardid] [nvarchar](50) NULL, [count] [int] NULL, [playerid] [int] NULL )
INSERT INTO [dbo].[yourtable] ([rewardid] ,[count] ,[playerid])
VALUES ('Burger King', 5,1), ('Burger King', 4,2), ('KFC',1,1), ('KFC',6,2) GO
它给了我结果
rewardid count playerid
Burger King 5 1
KFC 6 2
答案 1 :(得分:0)
select yourtable.rewardid,yourtable.count,yourtable.playerid from yourtable left join (select distinct rewardid, max(count) OVER (PARTITION BY rewardid) as maxcount from yourtable) source
on source.rewardid=yourtable.rewardid and source.maxcount=yourtable.count where source.maxcount is not null
这个答案假设我们想要每个奖励ID的记录
答案 2 :(得分:0)
select t.rewardId,t.count,playerId from tableName as t
join
(select rewardId,MAX([count]) as cnt
from tableName
group by rewardId
) as t1
on t.rewardId=t1.rewardId and t.count=t1.cnt