我試圖找出行是否具有組中的最大值。這裏是非常簡單的例子:檢查該行是否具有組中的最大值
數據
VoteCount LocationId UserId
3 1 1
4 1 2
3 2 2
4 2 1
僞查詢
select
LocationId,
sum(case
when UserId = 1 /* and has max vote count*/
then 1 else 0
end) as IsUser1Winner,
sum(case
when UserId = 2 /* and has max vote count*/
then 1 else 0
end) as IsUser2Winner
from LocationVote
group by LocationID
它應該返回:
LocationId IsUser1Winner IsUser2Winner
1 0 1
2 1 1
我也無法找到一個方法來生成動態列這裏的名字。寫這個查詢最簡單的方法是什麼?