排名

用户解题统计

过去一年提交了

勋章 ①金银铜:在竞赛中获得第一二三名;②好习惯:自然月10天提交;③里程碑:解决1/2/5/10/20/50/100/200题;④每周打卡挑战:完成每周5题,每年1月1日清零。

收藏

收藏日期 题目名称 解决状态
2024-12-01 小结(2)越花越多是死罪,按月统计Substr  已解决

评论笔记

评论日期 题目名称 评论内容 站长评论
2024-12-18 窗口函数(3)越来越喜欢召妓,窗口函数用累计(1) 
select t.trx_mon,sum(t.trx_amt)over(order by t.trx_mon) as trx_amt
from(
select date_format(a.trx_time,'%Y-%m') as trx_mon, sum(a.trx_amt) as trx_amt 
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
where usr_id = 5201314520 
and (date_format(trx_time,'%Y-%m') between '2023-01' and '2024-12')
and mch_typ = '休闲娱乐'
group by trx_mon)t
请问这个哪里错了呀
select date_format(a.trx_time,'%Y-%m') as trx_mon,  这句后面的逗号是中文还是英文?
2024-12-01 字符串与通配符(2)好多关键词做规则,可以使用rlike 
好的谢谢~
啥也没说
2024-11-30 字符串与通配符(2)好多关键词做规则,可以使用rlike 
lower(mch_nm) rlike '.*(按摩|保健|休闲|spa|养生|会所).*' 这个语句是什么呀,lower不是字母变小写吗,但mch_nm不是汉字吗;然后rlike和后面的'.*(按摩|保健|休闲|spa|养生|会所).*'都是什么写法呀,没见过
mch_nm里面有字母呀。SPA、ZARA之类的,得把SPA转成小写,再去匹配SPA;
如果你要匹配100个词,写100次:mch_nm like '%养生%' or mch_nm like '%会所%'…… 人都累傻了
rlike可以实现所有关键词写在一个语句里,用|隔开即可。

语句更简单易读

提交记录

提交日期 题目名称 提交代码
2025-03-03 给商品打四类标签(行) 
select gd.gd_id, gd.gd_nm, gd.gd_typ,
 case when fav.mch_id is null and pchs.mch_id is null then '未被收藏也未被购买的'
 when fav.mch_id is not null and pchs.mch_id is null then '只被收藏未被购买过的'
 when fav.mch_id is null and pchs.mch_id is not null then '只被购买未被收藏过的'
 else '既被收藏也被购买的' end as category
from gd_inf gd left join xhs_fav_rcd fav on gd.gd_id = fav.mch_id
 left join xhs_pchs_rcd pchs on gd.gd_id = pchs.mch_id
group by gd.gd_id, gd.gd_nm, gd.gd_typ
order by gd.gd_id
2025-03-03 只被购买未被收藏的商品 
select distinct gd.gd_id, gd.gd_nm, gd.gd_typ
from xhs_pchs_rcd pchs left join xhs_fav_rcd fav on pchs.mch_id = fav.mch_id
 left join gd_inf gd on pchs.mch_id = gd.gd_id
where fav.mch_id is null
2025-03-03 只被购买未被收藏的商品 
select gd.gd_id, gd.gd_nm, gd.gd_typ
from xhs_pchs_rcd pchs left join xhs_fav_rcd fav on pchs.mch_id = fav.mch_id
 left join gd_inf gd on pchs.mch_id = gd.gd_id
where fav.mch_id is null
2025-03-03 只被收藏未被购买的商品 
select gd.gd_id, gd.gd_nm, gd.gd_typ
from xhs_fav_rcd fav left join gd_inf gd on gd.gd_id = fav.mch_id
 left join xhs_pchs_rcd pchs on gd.gd_id = pchs.mch_id
where pchs.mch_id is null
group by gd.gd_id, gd.gd_nm, gd.gd_typ
2025-03-03 购买人数最多的商品类目 
select gd.gd_typ, count(distinct pchs.cust_uid) as buyer_count
from gd_inf gd join xhs_pchs_rcd pchs on gd.gd_id = pchs.mch_id
group by gd.gd_typ
order by buyer_count desc
limit 1
2025-03-03 被收藏次数最多的商品 
select gd.gd_id, gd.gd_nm, count(fav_trq) as fav_count
from xhs_fav_rcd fav join gd_inf gd on gd.gd_id = fav.mch_id
group by gd.gd_id, gd.gd_nm
order by fav_count desc
limit 1
2025-03-03 被收藏次数最多的商品 
select gd.gd_id, gd.gd_nm, count(fav_trq) as fav_count
from xhs_fav_rcd fav join gd_inf gd on gd.gd_id = fav.mch_id
group by gd.gd_id, gd.gd_nm
order by fav_count desc
2025-02-20 窗口函数(2)所有前一和每类前一,继续搞懂排序窗口函数 
with count_all as(
select 
'all' as mch_typ,
mch_nm,
count(trx_amt) as trx_cnt,
dense_rank()over(order by count(trx_amt) desc) as rnk
from cmb_usr_trx_rcd
where usr_id = 5201314520
group by mch_nm
),
count_each as(
select 
 b.mch_typ,
 b.mch_nm,
 count(trx_amt) as trx_cnt,
 dense_rank()over(partition by mch_typ order by count(trx_amt) desc )as rnk
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
where usr_id = 5201314520 and b.mch_typ in ('交通出行','休闲娱乐','咖啡奶茶')
group by b.mch_typ, b.mch_nm),
allrnnk as(
select 
mch_typ,
mch_nm,
trx_cnt,
rnk
from count_all
where rnk<=1
),
eachrnk as(
select 
mch_typ,
mch_nm,
trx_cnt,
rnk
from count_each
where rnk <= 1
)
select 
mch_typ,
mch_nm,
trx_cnt,
rnk
from allrnnk
union all
select
mch_typ,
mch_nm,
trx_cnt,
rnk
from eachrnk
order by mch_typ, rnk, mch_nm, trx_cnt
2025-02-19 窗口函数(1)年度前三和每月前三,搞懂排序窗口函数 
with yearly_rn as(
select 
 '2024' as trx_mon,
 mch_nm,
 sum(trx_amt) as 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
limit 3
),
monthly_merchants as 
(select 
 date_format(trx_time,'%Y-%m') as trx_mon,
 mch_nm,
 sum(trx_amt) as sum_trx_amt,
 row_number()over(partition by date_format(trx_time,'%Y-%m') order by sum(trx_amt) desc) as rn
 from 
 cmb_usr_trx_rcd
 where 
 usr_id = 5201314520 
 and year(trx_time) = 2024
 group by 
 trx_mon,mch_nm
),
monthly_rn as 
(select 
 trx_mon, mch_nm, sum_trx_amt
from monthly_merchants
where rn <= 3
)
select trx_mon, mch_nm, sum_trx_amt
from yearly_rn
union all
select trx_mon, mch_nm, sum_trx_amt
from monthly_rn
2025-02-19 时间日期(6)爽完来根事后烟,不羡鸳鸯不羡仙 
with first_time as(
select min(trx_time) as first_trx_time
from cmb_usr_trx_rcd
where usr_id = 5201314520 and mch_nm like '%红玫瑰%'
)
select usr_id, mch_nm, trx_time, trx_amt
from cmb_usr_trx_rcd
where usr_id = 5201314520 and trx_time between (select first_trx_time from first_time) and (select date_add(first_trx_time,interval 2 hour) from first_time)
order by trx_time
2025-02-19 时间日期(5)三腿爱往会所走,全当良心喂了狗 
select '2022-10-03 17:20:20' as time_he_love_me,
 datediff(current_date,'2022-10-03') as days_we_falling_love,
 timestampdiff(hour,'2022-10-03 17:20:20',now()) as hours_we_falling_love,
 datediff(min(trx_time),'2022-10-03') as days_he_fvck_else
from cmb_usr_trx_rcd
where usr_id = 5201314520 and mch_nm = '红玫瑰按摩保健休闲'
2025-02-19 时间日期(5)三腿爱往会所走,全当良心喂了狗 
select '2022-10-03 05:20:20' as time_he_love_me,
 datediff(current_date,'2022-10-03') as days_we_falling_love,
 timestampdiff(hour,'2022-10-03 17:20:20',now()) as hours_we_falling_love,
 datediff(min(trx_time),'2022-10-03') as days_he_fvck_else
from cmb_usr_trx_rcd
where usr_id = 5201314520 and mch_nm = '红玫瑰按摩保健休闲'
2025-02-19 时间日期(5)三腿爱往会所走,全当良心喂了狗 
select '2022-10-03 05:20:20' as time_he_love_me,
 datediff(current_date,'2022-10-03') as days_we_falling_love,
 timestampdiff(hour,'2022-10-03 05:20:20',now()) as hours_we_falling_love,
 datediff(min(trx_time),'2022-10-03') as days_he_fvck_else
from cmb_usr_trx_rcd
where usr_id = 5201314520 and mch_nm = '红玫瑰按摩保健休闲'
2025-02-19 时间日期(5)三腿爱往会所走,全当良心喂了狗 
select '2022-10-03 05:20:20' as time_he_love_me,
 datediff(current_date,'2022-10-03') as days_we_falling_love,
 timestampdiff(hour,'2022-10-03 05:20:20',now()) as hours_we_falling_love,
 datediff(min(trx_time),current_date) as days_he_fvck_else
from cmb_usr_trx_rcd
where usr_id = 5201314520 and mch_nm = '红玫瑰保健休闲'
2025-02-17 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select u.trx_mon,
 coalesce(t.last_day,'1900-01-01') as last_day,
 coalesce(t.day_of_mon,0) as day_of_mon,
 coalesce(t.trx_amt,0) as trx_amt,
 coalesce(t.trx_cnt,0) as trx_cnt,
 coalesce(round(t.avg_day_amt,2),0) as avg_day_amt,
 coalesce(round(t.avg_day_cnt,2),0) as avg_day_cnt
from (select distinct date_format(date_value,'%Y-%m') as trx_mon
from date_table
where date_format(date_value,'%Y-%m') between '2023-01' and '2024-06')u 
left join
(select date_format(trx_time,'%Y-%m') as trx_mon,
 last_day(max(trx_time)) as last_day,
 day(last_day(max(trx_time))) as day_of_mon,
 sum(trx_amt) as trx_amt,
 count(trx_amt) as trx_cnt,
 sum(trx_amt)/day(last_day(max(trx_time))) as avg_day_amt,
 count(trx_amt)/day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a left join cmb_mch_typ b on a.mch_nm = b.mch_nm
wherea.usr_id = 5201314520
       AND DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
       AND (b.mch_typ = '休闲娱乐' OR b.mch_typ IS NULL)
       AND HOUR(a.trx_time) IN (23, 0, 1, 2)
       AND a.trx_amt >= 288
group by trx_mon
order by trx_mon)t 
on u.trx_mon = t. trx_mon
order by u.trx_mon
2025-02-17 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select u.trx_mon,
 coalesce(t.last_day,'1900-01-01') as last_day,
 coalesce(t.day_of_mon,0) as day_of_mon,
 coalesce(t.trx_amt,0) as trx_amt,
 coalesce(t.trx_cnt,0) as trx_cnt,
 coalesce(round(t.avg_day_amt,2),0) as avg_day_amt,
 coalesce(round(t.avg_day_cnt,2),0) as avg_day_cnt
from (select distinct date_format(date_value,'%Y-%m') as trx_mon
from date_table
where date_format(date_value,'%Y-%m') between '2023-01' and '2024-06')u 
left join
(select date_format(trx_time,'%Y-%m') as trx_mon,
 last_day(max(trx_time)) as last_day,
 day(last_day(max(trx_time))) as day_of_mon,
 sum(trx_amt) as trx_amt,
 count(trx_amt) as trx_cnt,
 sum(trx_amt)/day(last_day(max(trx_time))) as avg_day_amt,
 count(trx_amt)/day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
wherea.usr_id = 5201314520
       AND DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
       AND (b.mch_typ = '休闲娱乐' OR b.mch_typ IS NULL)
       AND HOUR(a.trx_time) IN (23, 0, 1, 2)
       AND a.trx_amt >= 288
group by trx_mon
order by trx_mon)t 
on u.trx_mon = t. trx_mon
order by u.trx_mon
2025-02-17 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select u.trx_mon,
 coalesce(t.last_day,'1900-01-01') as last_day,
 coalesce(t.day_of_mon,0) as day_of_mon,
 coalesce(t.trx_amt,0) as trx_amt,
 coalesce(t.trx_cnt,0) as trx_cnt,
 coalesce(round(t.avg_day_amt,2),0) as avg_day_amt,
 coalesce(round(t.avg_day_cnt,2),0) as avg_day_cnt
from (select distinct date_format(date_value,'%Y-%m') as trx_mon
from date_table
where date_format(date_value,'%Y-%m') between '2023-01' and '2024-06')u 
left join
(select date_format(trx_time,'%Y-%m') as trx_mon,
 last_day(max(trx_time)) as last_day,
 day(last_day(max(trx_time))) as day_of_mon,
 sum(trx_amt) as trx_amt,
 count(trx_amt) as trx_cnt,
 sum(trx_amt)/day(last_day(max(trx_time))) as avg_day_amt,
 count(trx_amt)/day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
where usr_id = 5201314520
and hour(a.trx_time) in (23,0,1,2)
and a.trx_amt > 288
and (b.mch_typ = '休闲娱乐' or b.mch_typ is null)
and DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
group by trx_mon
order by trx_mon)t 
on u.trx_mon = t. trx_mon
order by u.trx_mon
2025-02-17 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select u.trx_mon,
 coalesce(t.last_day,'1900-01-01') as last_day,
 coalesce(t.day_of_mon,0) as day_of_mon,
 coalesce(t.trx_amt,0) as trx_amt,
 coalesce(t.trx_cnt,0) as trx_cnt,
 coalesce(round(t.avg_day_amt,2),0) as avg_day_amt,
 coalesce(round(t.avg_day_cnt,2),0) as avg_day_cnt
from (select distinct date_format(date_value,'%Y-%m') as trx_mon
from date_table
where date_format(date_value,'%Y-%m') between '2023-01' and '2024-06')u 
left join
(select date_format(trx_time,'%Y-%m') as trx_mon,
 last_day(max(trx_time)) as last_day,
 day(last_day(max(trx_time))) as day_of_mon,
 sum(trx_amt) as trx_amt,
 count(trx_amt) as trx_cnt,
 sum(trx_amt)/day(last_day(max(trx_time))) as avg_day_amt,
 count(trx_amt)/day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
where usr_id = 5201314520
and hour(a.trx_time) in (23,0,1,2)
and a.trx_amt > 288
and (b.mch_typ = '休闲娱乐' or b.mch_typ is null)
and DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
group by trx_mon
order by trx_mon)t 
on u.trx_mon = t. trx_mon
order by t.trx_mon
2025-02-17 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select u.trx_mon,
 coalesce(t.last_day,'1900-01-01') as last_day,
 coalesce(t.day_of_mon,0) as day_of_mon,
 coalesce(t.trx_amt,0) as trx_amt,
 coalesce(t.trx_cnt,0) as trx_cnt,
 coalesce(round(t.avg_day_amt,2),0) as avg_day_amt,
 coalesce(round(t.avg_day_cnt,2),0) as avg_day_cnt
from (select distinct date_format(date_value,'%Y-%m') as trx_mon
from date_table
where date_format(date_value,'%Y-%m') between '2023-01' and '2024-06')u 
left join
(select date_format(trx_time,'%Y-%m') as trx_mon,
 last_day(max(trx_time)) as last_day,
 day(last_day(max(trx_time))) as day_of_mon,
 sum(trx_amt) as trx_amt,
 count(trx_amt) as trx_cnt,
 sum(trx_amt)/day(last_day(max(trx_time))) as avg_day_amt,
 count(trx_amt)/day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
where hour(a.trx_time) in (23,0,1,2)
and a.trx_amt > 288
and (b.mch_typ = '休闲娱乐' or b.mch_typ is null)
and DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
group by trx_mon
order by trx_mon)t 
on u.trx_mon = t. trx_mon
order by t.trx_mon
2025-02-17 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select u.trx_mon,
 coalesce(t.last_day,'1900-01-01') as last_day,
 coalesce(t.day_of_mon,0) as day_of_mon,
 coalesce(t.trx_amt,0) as trx_amt,
 coalesce(t.trx_cnt,0) as trx_cnt,
 coalesce(t.avg_day_amt,0) as avg_day_amt,
 coalesce(t.avg_day_cnt,0) as avg_day_cnt
from (select distinct date_format(date_value,'%Y-%m') as trx_mon
from date_table
where date_format(date_value,'%Y-%m') between '2023-01' and '2024-06')u 
left join
(select date_format(trx_time,'%Y-%m') as trx_mon,
 last_day(max(trx_time)) as last_day,
 day(last_day(max(trx_time))) as day_of_mon,
 sum(trx_amt) as trx_amt,
 count(trx_amt) as trx_cnt,
 sum(trx_amt)/day(last_day(max(trx_time))) as avg_day_amt,
 count(trx_amt)/day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a join cmb_mch_typ b on a.mch_nm = b.mch_nm
where hour(trx_time) in (23,0,1,2)
and trx_amt >= 288
and (b.mch_typ = '休闲娱乐' or b.mch_typ is null)
and DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
group by trx_mon
order by trx_mon)t 
on u.trx_mon = t. trx_mon
order by t.trx_mon