【发布时间】:2020-04-13 07:42:32
【问题描述】:
这是我对the other question 的跟进。
Quarter Segment Customer *Counter*
Q1 2018 A A1 1
Q2 2018 A A1 2
Q3 2018 A A1 3
Q4 2018 B A1 1
Q1 2019 B A1 2
Q2 2019 A A1 1
Q1 2020 A A1 *1* I want 1 not 2 here because it's not consecutive (we don't have Q3 & Q4 2019)
Q2 2020 A A1 *2* I want 2 not 3 here because it reset in Q1 2020.
如果日期是连续的,则以下查询有效。我将如何调整查询以获得我正在寻找的内容?我尝试添加一个滞后 1 行的新列,并尝试检查新列中的季度值是否本质上是真实的前 1 个季度(如果是,使用计数器,如果不是,使用“1”重新启动)。但它不会重置以下记录。那我不知道该怎么办了。
select quarter, customer, segment,
row_number() over (partition by customer, segment, seqnum - seqnum_cs order by right(quarter, 4), left(quarter, 2)) as counter
from (select t.*,
row_number() over (partition by customer order by right(quarter, 4), left(quarter, 2)) as seqnum,
row_number() over (partition by customer, segment order by right(quarter, 4), left(quarter, 2)) as seqnum_cs
from t
) t
order by customer, seqnum;
【问题讨论】:
标签: sql database amazon-redshift window-functions gaps-and-islands