分组分组

时间:2018-07-23 12:50:46

标签: teradata

我的桌子:

select * from A

enter image description here

我想要的输出:

根据观看次数的降序,每个用户ID每月排名前2的类别。

以表格形式将是:

enter image description here

我尝试了这个,但是我不太明白我在做什么错

select userid, category, month, views
from A
qualify
row_number() over (partition by userid, category, month order by views desc) = 2

1 个答案:

答案 0 :(得分:0)

您是说这个意思吗?

select userid, category, month, views
from A
qualify
row_number() over (partition by userid, month order by views desc) <= 2