菜鸡做了很久想不出来,参考答案的思路后才做出来,记录一下:
/*全量日期表*/
with full_day_list as (
select
date_value
from
date_table
where
year(date_value) = 2024
and month(date_value) = 9
order by
date_value
),
/*计算基本type次数*/
time_trx_type as (
select
date(trx_time) as dt,
count(1) as fvck_cnt,
sum(case when trx_amt = 288 then 1 else 0 end) as WithHand,
sum(case when trx_amt = 388 then 1 else 0 end) as WithBalls,
sum(case when trx_amt = 588 then 1 else 0 end) as BlowJobbie,
sum(case when trx_amt = 888 then 1 else 0 end) as Doi,
sum(case when trx_amt = 1288 then 1 else 0 end) as DoubleFly
from
cmb_usr_trx_rcd
where
usr_id = 5201314520
and mch_nm rlike ('按摩|保健|休闲|会所')
and year(trx_time) = 2024
and month(trx_time) = 9
group by
date(trx_time)
order by
dt
),
/*ohya特殊分类次数的日期*/
oyha_dt as (
select
date(trx_time) as dt
from(
select
*,
lag(trx_time,1) over(partition by usr_id order by trx_time) as last_time,
lag(trx_amt,1) over(partition by usr_id order by trx_time) as last_amt
from
cmb_usr_trx_rcd
where
usr_id = 5201314520
and mch_nm rlike ('按摩|保健|休闲|会所')
and year(trx_time) = 2024
and month(trx_time) = 9
) as a
where
trx_amt = 888
and last_amt = 1288
and date(trx_time) = date(last_time)
)
/*合并日期,整合ohya列数据*/
select
a.date_value,
coalesce(b.fvck_cnt,0) as fvck_cnt,
coalesce(b.WithHand,0) as WithHand,
coalesce(b.WithBalls,0) as WithBalls,
coalesce(b.BlowJobbie,0) as BlowJobbie,
coalesce(b.Doi,0) as Doi,
coalesce(b.DoubleFly,0) as DoubleFly,
case when c.dt is not null then 1 else 0 end as ohya
from
full_day_list as a
left join
time_trx_type as b on a.date_value = b.dt
left join
oyha_dt as c on a.date_value = c.dt
order by
a.date_value
/*整体解题思路:
① 先得到完整的日期列表
② 再得到基础type的次数,每种分类按列统计
③ 再单独计算出ohya的统计列
(难点:先Doi后Double怎么实现;
-- 求出下次消费金额 和 下次消费时间
-- 本次消费金额=888,下次消费金额=1288
-- 且要求2次消费日期为同一天)
④ 最后将①左联②左连③,注意处理null值转换
*/
select a.cust_uid, c.cust_uid from
(
select cust_uid, mch_nm from mt_trx_rcd1 where cust_uid='MT10000' group by cust_uid, mch_nm) a
left join
(
select cust_uid, mch_nm from mt_trx_rcd1 group by cust_uid, mch_nm
)c
on a.cust_uid <> c.cust_uid and a.mch_nm = c.mch_nm
group by a.cust_uid, c.cust_uid
select
'MT10000' as cust_uid,cust_uid as cust_uid_1 ,mch_nm
from
mt_trx_rcd1
where
mch_nm='兰州李晓明拉面馆'and cust_uid != 'MT10000'
group by
cust_uid, mch_nm
order by 2
select
'MT10000' as cust_uid,cust_uid as cust_uid_1 ,mch_nm
from
mt_trx_rcd1
where
mch_nm='兰州李晓明拉面馆'and mch_nm != 'MT10000'
group by
cust_uid, mch_nm
order by 2
select
case
when
(truncate(trx_amt, 0) like '%88' or truncate(trx_amt, 0) like '%98') and
trx_amt >= 200 and
(hour(trx_time) between 0 and 3 or hour(trx_time) = 23)
then 'illegal'
else 'other'
end as trx_typ,
count(1) as trx_cnt,
sum(trx_amt) as trx_amt,
count(distinct mch_nm) as mch_cnt
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
group by
1
order by
trx_cnt desc;
select
case
when (truncate(trx_amt, 0) like '%88' or truncate(trx_amt, 0) like '%98')
and hour(trx_time) in (23, 00, 01, 02, 03)
then 'illegal'
else 'other'
end as trx_typ,
count(1) trx_cnt,
sum(trx_amt) trx_amt,
count(distinct mch_nm) mch_nm
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
and trx_amt > 200
group by
1
order by
1 desc;
select
case
when mch_nm like '%按摩保健休闲%' then '按摩保健休闲'
when lower(mch_nm) rlike '.*(按摩|保健|休闲|spa|养生|会所).*' then '按摩、保健、休闲、养生、SPA、会所'
end as reg_rules,
count(distinct mch_nm) as mch_cnt
from
cmb_usr_trx_rcd
where mch_nm like '%按摩保健休闲%'
or lower(mch_nm) rlike '.*(按摩|保健|休闲|spa|养生|会所).*'
group by reg_rules
order by mch_cnt desc;
select
case
when trx_amt = 288 then '1.WithHand'
when trx_amt = 388 then '2.WithMimi'
when trx_amt = 588 then '3.BlowJobbie'
when trx_amt = 888 then '4.Doi'
when trx_amt = 1288 then '5.DoubleFly'
else '6.other'
end as ser_typ,
count(1) trx_cnt,
min(date(trx_time)) first_date
from
cmb_usr_trx_rcd
where
mch_nm = '红玫瑰按摩保健休闲'
and usr_id = '5201314520'
group by
1
order by
1
select
case
when trx_amt = 288 then '1.WithHand'
when trx_amt = 388 then '2.WithMimi'
when trx_amt = 588 then '3.BlowJobbie'
when trx_amt = 888 then '4.Doi'
when trx_amt = 1288 then '5.DoubleFly'
else 'other'
end as ser_typ,
count(1) trx_cnt,
min(date(trx_time)) first_date
from
cmb_usr_trx_rcd
where
mch_nm = '红玫瑰按摩保健休闲'
and usr_id = '5201314520'
group by
1
order by
1
select
usr_id,
mch_nm,
sum(trx_amt) trx_amt,
count(1) trx_cnt,
min(trx_time) first_time
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
and trx_amt >= 288
group by
1,2
order by
trx_cnt desc
select
trx_amt,
count(1) total_trx_cnt,
count(distinct usr_id) unique_usr_cnt,
round(count(1)/count(distinct usr_id), 4) avg_trx_per_user
from
cmb_usr_trx_rcd
where
mch_nm = '红玫瑰按摩保健休闲'
and date(trx_time) between '2023-01-01' and '2024-06-30'
group by
trx_amt
order by
avg_trx_per_user desc
limit 5
select
trx_amt,
count(1) trx_cnt
from
cmb_usr_trx_rcd
where
mch_nm = '红玫瑰按摩保健休闲'
and date(trx_time) between '2024-01-01' and '2024-07-31'
group by
trx_amt
order by
trx_cnt desc
limit 5
select
date(trx_time) trx_date,
max(trx_amt) max_trx_amt,
min(trx_amt) min_trx_amt,
avg(trx_amt) avg_trx_amt,
sum(trx_amt) total_trx_amt
from
cmb_usr_trx_rcd
where
mch_nm = '红玫瑰按摩保健休闲'
and year(trx_time) = 2024
and month(trx_time) = 9
group by
1
order by
1
select
mch_nm,
sum(trx_amt) sum_trx_amt
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
and year(trx_time) = 2024
group by
mch_nm
order by
sum_trx_amt desc
select
*
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
and date(trx_time) between '2024-09-01'and '2024-09-30'
and Hour(trx_time)in (22,23,0,1,2,3,4,5)
order by
trx_time
select
*
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
and date(trx_time) between '2024-09-01'and '2024-09-30'
and Hour(trx_time)in (22,23,0,1,2,3,4,5,6)
order by
trx_time
select
*
from
cmb_usr_trx_rcd
where
usr_id = '5201314520'
and date(trx_time) between '2024-09-01'and '2024-09-30'
and hour(trx_time) between '01:00:00'and '05:00:00'
order by
trx_time