10. 分类(1)姿势太多很过分,分类要用CaseWhen
之前我们研究“红玫瑰按摩保健休闲”过这个商户,知道了他们店有哪些常见项目。
这玩意儿有瘾,一般都是逐渐升级的,从最开始的扭扭捏捏不好意思,到最后愉快阈值越来越高。DoubleFly都不一定能满足,得Dirty Party了。
你闺蜜想知道,渣男在“红玫瑰按摩保健休闲”,每个项目都分别做过几次,第一次做什么时候。
输出ser_typ(服务类型,由服务金额case when 映射而来)、trx_cnt(交易次数)、first_date(初尝日期)三个字段,按照服务类型排序。
映射规则如下:
288-1.WithHand
388-2.WithMimi
588-3.BlowJobbie
888-4.Doi
1288-5.DoubleFly
ser_typ | trx_cnt | first_date |
---|---|---|
1.WithHand | 7 | 2023-01-06 |
2.WithMimi | 3 | 2022-12-15 |
3.BlowJobbie | 4 | 2022-12-25 |
4.Doi | 28 | 2022-11-17 |
5.DoubleFly | 7 | 2023-10-07 |
6.other | 33 | 2023-10-09 |
cmb_usr_trx_rcd,支付明细表
usr_id | mch_nm | trx_time | trx_amt |
---|---|---|---|
5201314520 | 肯德基 | 2023-10-01 12:23:24 | 49 |
5211414521 | 丝芙源网络 | 2024-03-13 17:23:23 | 123 |
请输出ser_typ(服务类型)、trx_cnt(交易次数)、first_date(初尝日期)三个字段,按照服务类型排序。
ser_typ | trx_cnt | first_date |
---|---|---|
1.WithHand | 7 | 2023-01-06 |
2.WithMimi | 3 | 2022-12-15 |
3.BlowJobbie | 4 | 2022-12-25 |
4.Doi | 28 | 2022-11-17 |
5.DoubleFly | 7 | 2023-10-07 |
6.other | 33 | 2023-10-09 |
点击下方空白区域即可查看参考答案
/*CaseWhen其实就是打标签,这次我们先尝试最简单的打标签,只有一个条件*/
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) as trx_cnt
,min(date(trx_time)) as first_date
from
cmb_usr_trx_rcd
where usr_id='5201314520' and mch_nm='红玫瑰按摩保健休闲'
group by ser_typ
order by 1
MySQL 8.0
xxxxxxxxxx
2
select * from cmb_usr_trx_rcd limit 5;