我有一张如下表:
group sequence action
1 1 promotion_1
1 2 promotion_1
1 3 promotion_2
1 4 act1
1 5 act1
1 6 act2
1 7 promotion_1
1 8 act1
1 9 act2
1 10 promotion_1
1 11 act1
2 1 promotion_2
2 2 act1
我想创建一个排名,它将显示促销范围。因此,我需要在促销后获得相同的促销和每个行动的号码:
group sequence action parameter
1 1 promotion_1 1
1 2 promotion_1 2
1 3 promotion_2 3
1 4 act1 3
1 5 act1 3
1 6 act2 3
1 7 promotion_1 4
1 8 act1 4
1 9 act2 4
1 10 promotion_1 5
1 11 act1 5
2 1 promotion_2 1
2 2 act1 1
我读到了窗口函数及其可能性(例如over()),但我只能通过“action”创建group with ranking。
答案 0 :(得分:2)
select T.*,
sum(case when action like 'promotion%' then 1 else 0 end)
over(partition by "group" order by sequence rows unbounded preceding) parameter
from Table T
答案 1 :(得分:1)
var userId = Apperyio.get('userManager').userId;
(从PostgeSQL 9.4开始)
filter
select *
,count (*) filter (where action like 'promotion%') over
(
partition by "group"
order by sequence
rows unbounded preceding
)
from mytable T