我试图找出行是否具有组中的最大值。这里是非常简单的例子:检查该行是否具有组中的最大值
数据
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
我也无法找到一个方法来生成动态列这里的名字。写这个查询最简单的方法是什么?