sample_input_table
user name action date
1 aaa view 2020-09-03
2 bbb view 2020-09-02
3 ccc view 2020-08-28
4 ddd view 2020-08-25
1 aaa purchase 2020-09-09
我有一个表,其中有很多行,该表看起来像上面。
题
purchase
动作的行,purchase
必须有view
动作view
操作将处于purchase_date(2020-09-09
)和purchase_date-7days(2020-09-02
)的日期范围内。我想在一个sql查询中实现这3点
sample_output
user name action date
1 aaa purchase 2020-09-09
如果我们从样本输入中看到样本输出
view
采取行动view
在2020-09-09
和2020-09-02
(购买日期,购买日期-7天)的时间范围内有人可以为此建议一些解决方案吗?
您可以使用exists
:
select t.*
from mytable t
where t.action = 'purchase' and exists (
select 1
from mytable t1
where
t1.user = t.user
and t1.action = 'view'
and t1.date >= t.date - interval '7' day
and t1.date < t.date
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句