with tmp_1 as (
select cust_id
from trx_act
where trx_date between'2022-01-01'and'2022-01-31'groupby cust_id
),tmp_2 as (
select cust_id
from trx_act
where trx_date between'2022-02-01'and'2022-02-29'groupby cust_id
),
.....
,tmp_12 as (
select cust_id
from trx_act
where trx_date between'2022-12-01'and'2022-12-31'groupby cust_id
)
select a.cust_id
from tmp_1 a
leftjoin tmp_2 b
on a.cust_id = b.cust_id
leftjoin tmp_3 con a.cust_id =c.cust_id
where b.cust_id isnotnullandc.cust_id isnotnull
.....
当然,我这里又想出了两种比较好的方法:
123456789101112131415161718192021222324
-- 方法1select cust_id,action,count(*) as times
from (
select cust_id,action,(month- rank_num) as flag
from (
select cust_id,action,month,rank() over(partion by cust_id,actionorderbymonth) as rank_num
from tbl
groupbymonth,action,cust_id a
)b
)
groupby cust_id,action,flag
having times >=3;
-- 方法二selectdistinct cust_id,actionfrom (
select cust_id
,action
,month
,lag(month,2) over(partitionby cust_id,actionorderbymonth) as lag_2_month
from tbl
groupbymonth,cust,action
)a
wheremonth- lag_2_month =2; -- 某一行的月份 - 前两行的月份的差值等于2