答案 0 :(得分:1)
如果用户在两个poscode上花费的时间相同,则这将输出两个记录:
select
UserID,
Postcode,
Hours
from
(
select
UserID,
Postcode,
Hours,
dense_rank() over(partition by userId order by hours desc) rn
from (select --skip this subquery if hours already aggregated by user, postcode
UserID, Postcode, sum(Hours) hours
from table group by UserID, Postcode
) s
)s
where rn = 1;
如果每个用户需要一条记录,请使用row_number() over(partition by userId order by hours desc) rn
代替dense_rank()
。