Sybase IQ 16 - 查找更改产品的成员
Sybase IQ 16 - Find members who have changed product
我的数据记录了会员购买某类产品的历史记录。通常情况下,会员会购买产品 X 几个月,但我对那些转向产品 Y 的会员以及购买时间很感兴趣。
我的数据如下:
Member Number Date Product TOT
210539662 2019-05-26 PRODUCT A PREVENTATIVE
210539662 2019-06-28 PRODUCT A PREVENTATIVE
210539662 2019-07-30 PRODUCT A PREVENTATIVE
210539662 2019-08-28 PRODUCT A PREVENTATIVE
210539662 2019-09-24 PRODUCT A PREVENTATIVE
210539662 2019-10-17 PRODUCT A PREVENTATIVE
210539662 2019-11-19 PRODUCT B TREATMENT
210539662 2019-12-20 PRODUCT B TREATMENT
我想列出所有会员编号以及他们从预防产品转换为治疗产品的第一天。
请注意我正在使用 Sybase IQ,我不相信它可能 运行 有序子查询。
I would like to pull a list of all member numbers and the first date they switched from preventative products to treatment products.
使用lag()
和group by
:
select member,
min(case when prev_tot = 'PREVENTATIVE' and tot = 'TREATMENT'
then date
end) as first_prev_to_treat_date
from (select t.*,
lag(tot) over (partition by member order by date) as prev_tot
from t
) t
group by member;
我的数据记录了会员购买某类产品的历史记录。通常情况下,会员会购买产品 X 几个月,但我对那些转向产品 Y 的会员以及购买时间很感兴趣。
我的数据如下:
Member Number Date Product TOT
210539662 2019-05-26 PRODUCT A PREVENTATIVE
210539662 2019-06-28 PRODUCT A PREVENTATIVE
210539662 2019-07-30 PRODUCT A PREVENTATIVE
210539662 2019-08-28 PRODUCT A PREVENTATIVE
210539662 2019-09-24 PRODUCT A PREVENTATIVE
210539662 2019-10-17 PRODUCT A PREVENTATIVE
210539662 2019-11-19 PRODUCT B TREATMENT
210539662 2019-12-20 PRODUCT B TREATMENT
我想列出所有会员编号以及他们从预防产品转换为治疗产品的第一天。
请注意我正在使用 Sybase IQ,我不相信它可能 运行 有序子查询。
I would like to pull a list of all member numbers and the first date they switched from preventative products to treatment products.
使用lag()
和group by
:
select member,
min(case when prev_tot = 'PREVENTATIVE' and tot = 'TREATMENT'
then date
end) as first_prev_to_treat_date
from (select t.*,
lag(tot) over (partition by member order by date) as prev_tot
from t
) t
group by member;