排名

用户解题统计

过去一年提交了

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

收藏

收藏日期 题目名称 解决状态
没有收藏的题目。

评论笔记

评论日期 题目名称 评论内容 站长评论
2025-02-05 小结(2)越花越多是死罪,按月统计Substr 
2022年11月至2024年,为啥答案不包含22年1月1日呢
啥也没说
2025-02-05 小结(1)大数据早就能扫黄,找足证据不慌张 
你这实际答案输出和输出示例好像对不上哎,有点误导人哎
啥也没说
2025-02-05 字符串与通配符(2)好多关键词做规则,可以使用rlike 
这道题有个大坑,CASE WHEN的条件判断是需要有先后顺序的。。。我感觉这题出的挺莫名其妙的,业务逻辑上也有点怪,理论上两个条件是可以互相重叠才对,非要用CASE WHEN拆开
认真刷题👍

很多同学不知道case when 有先后顺序。
复制这个飞书链接,去看下其他同学的想法:

https://qxvb1cn8oj.feishu.cn/minutes/obcnph921nldwtxm68c2tns9
2025-02-05 分组与聚合函数(3)五花八门的项目,其实都有固定套路(1) 
建议改成:92,95,98 (手动狗头,懂得都懂)
啥也没说
2025-02-05 分组与聚合函数(2)擦边营收怎么样,聚合函数可看出 
答案与问题不符。。。。你问题里说明是要查这个渣男的,怎么不限定usr_id呢?
注意审题。擦边营收,是商家维度。
2025-02-05 条件过滤(1)异地男友有异常,数分闺蜜来帮忙 
我们平时还有一种写法,是先用substr把时间戳的日期前缀截取出来,这样就不用担心闭合不全的问题了:

SELECT A.usr_id,A.mch_nm, A.trx_time,A.trx_amt
FROM
(SELECT *,SUBSTR(trx_time,1,10) AS date_id
FROM cmb_usr_trx_rcd
WHERE usr_id = '5201314520' ) AS A
WHERE A.date_id BETWEEN '2024-09-01' AND '2024-09-30'
ORDER BY trx_time
啥也没说
2025-02-05 条件过滤(1)异地男友有异常,数分闺蜜来帮忙 
In SQL, the BETWEEN operator is inclusive, meaning it includes the boundary values. However, when comparing a timestamp with a date, the date is implicitly treated as the start of the day (i.e., '2024-09-30' is treated as '2024-09-30 00:00:00').

所以如果不想用date,2024-09-30就要改成2024-10-01。但是假如刚好有一条 2024-10-01 00:00:00,那么查询的结果也是错的。保险起见还是加上date比较好。这个太坑了,平时真的很少会留意这些细节
啥也没说

提交记录

提交日期 题目名称 提交代码
2025-05-03 窗口函数(1)年度前三和每月前三,搞懂排序窗口函数 
with a as (
select '2024' as trx_month,mch_nm,sum_trx_amt
from(
select mch_nm,sum(trx_amt) as sum_trx_amt
from cmb_usr_trx_rcd 
where year(trx_time) = 2024 and usr_id = '5201314520'
group by mch_nm) as a
order by sum_trx_amt desc
limit 3),
b as (
select b.*
from(
select a.*,row_number()over(partition by a.trx_month order by a.sum_trx_amt desc) as rank_num
from(
select substr(trx_time,1,7) as trx_month,mch_nm,sum(trx_amt) as sum_trx_amt
from cmb_usr_trx_rcd
where year(trx_time) = 2024 and usr_id = '5201314520'
group by substr(trx_time,1,7),mch_nm
) as a
) as b
where b.rank_num <= 3
)
select a.* from a
union all
select b.trx_month,b.mch_nm,b.sum_trx_amt from b
2025-05-03 窗口函数(1)年度前三和每月前三,搞懂排序窗口函数 
with a as (
select '2024' as trx_month,mch_nm,sum_trx_amt
from(
select mch_nm,sum(trx_amt) as sum_trx_amt
from cmb_usr_trx_rcd 
where year(trx_time) = 2024 and usr_id = '5201314520'
group by mch_nm) as a
order by sum_trx_amt desc
limit 3),
b as (
select b.*
from(
select a.*,row_number()over(partition by a.trx_month,mch_nm order by a.sum_trx_amt desc) as rank_num
from(
select substr(trx_time,1,7) as trx_month,mch_nm,sum(trx_amt) as sum_trx_amt
from cmb_usr_trx_rcd
where year(trx_time) = 2024 and usr_id = '5201314520'
group by substr(trx_time,1,7),mch_nm
) as a
) as b
where b.rank_num <= 3
)
select a.* from a
union all
select b.trx_month,b.mch_nm,b.sum_trx_amt from b
2025-05-03 时间日期(6)爽完来根事后烟,不羡鸳鸯不羡仙 
with time_temp as(
select min(trx_time) as first_trx_time
from cmb_usr_trx_rcd
where usr_id = '5201314520' and mch_nm = '红玫瑰按摩保健休闲'
)
select *
from cmb_usr_trx_rcd 
where usr_id = '5201314520' 
and trx_time between 
(select first_trx_time from time_temp)
and 
(select date_add(first_trx_time,interval 2 hour) from time_temp)
2025-05-03 时间日期(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(DATE(trx_time)), '2022-10-03') AS days_from_first_trx
FROM cmb_usr_trx_rcd 
WHERE usr_id = '5201314520' AND mch_nm = '红玫瑰按摩保健休闲'
2025-05-03 时间日期(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(DATE(trx_time)), '2022-10-03') AS days_from_first_trx
FROM cmb_usr_trx_rcd 
WHERE usr_id = '5201314520' AND mch_nm = '红玫瑰按摩保健休闲'
GROUP BY mch_nm
2025-05-03 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
SELECT 
    a.trx_mon, 
    COALESCE(b.last_day, '1900-01-01') AS last_day, 
    COALESCE(b.day_of_mon, 0) AS day_of_mon, 
    COALESCE(b.trx_amt, 0) AS trx_amt, 
    COALESCE(b.trx_cnt, 0) AS trx_cnt, 
    COALESCE(ROUND(b.avg_day_amt, 2), 0) AS avg_day_amt, 
    COALESCE(ROUND(b.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'
    ) a
LEFT JOIN
    (SELECT 
         DATE_FORMAT(a.trx_time, '%Y-%m') AS trx_mon, 
         LAST_DAY(MAX(a.trx_time)) AS last_day, 
         DAY(LAST_DAY(MAX(a.trx_time))) AS day_of_mon, 
         SUM(a.trx_amt) AS trx_amt, 
         COUNT(*) AS trx_cnt, 
         SUM(a.trx_amt) / DAY(LAST_DAY(MAX(a.trx_time))) AS avg_day_amt, 
         COUNT(*) / DAY(LAST_DAY(MAX(a.trx_time))) AS avg_day_cnt 
     FROM cmb_usr_trx_rcd a
     LEFT JOIN cmb_mch_typ m ON a.mch_nm = m.mch_nm
     WHERE a.usr_id = 5201314520
       AND DATE_FORMAT(a.trx_time, '%Y-%m') BETWEEN '2023-01' AND '2024-06'
       AND (m.mch_typ = '休闲娱乐' OR m.mch_typ IS NULL)
       AND HOUR(a.trx_time) IN (23, 0, 1, 2)
       AND a.trx_amt >= 288
     GROUP BY DATE_FORMAT(a.trx_time, '%Y-%m')
     ORDER BY 1
    ) b
ON a.trx_mon = b.trx_mon
ORDER BY a.trx_mon;
2025-05-03 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select 
a.trx_mon,
coalesce(b.last_day,'1900-01-01') as last_day,
coalesce(b.day_of_mon,'0') as day_of_mon,
coalesce(b.trx_amt,'0') as trx_amt,
coalesce(b.trx_cnt,'0') as trx_cnt,
coalesce(round(b.avg_day_amt,2),'0') as avg_day_amt,
coalesce(round(b.avg_day_cnt,2),'0') as avg_day_cnt
from(	
select distinct substr(date_value,1,7) as trx_mon
from date_table
where date_value between '2023-01-01' AND '2024-06-30'
	) as a
left join
(
select 
substr(a.trx_time,1,7) as trx_mon,
last_day(max(a.trx_time)) as last_day,
day(last_day(max(a.trx_time))) as day_of_mon,
sum(a.trx_amt) as trx_amt,
count(1) as trx_cnt,
sum(a.trx_amt)/day(last_day(max(trx_time)) ) as avg_day_amt,
count(1)/day(last_day(max(trx_time)) ) as avg_day_cnt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520' and date(a.trx_time) BETWEEN '2023-01-01' AND '2024-6-30'
and ((b.mch_typ='休闲娱乐'or b.mch_typ is null)
and a.trx_amt>=288
and hour(a.trx_time) in (23,0,1,2))
group by substr(a.trx_time,1,7)
order by 1 asc) as b
on a.trx_mon = b.trx_mon
2025-05-03 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select 
a.trx_mon,
coalesce(b.last_day,'1900-01-01') as last_day,
coalesce(b.day_of_mon,'0') as day_of_mon,
coalesce(b.trx_amt,'0') as trx_amt,
coalesce(b.trx_cnt,'0') as trx_cnt,
coalesce(b.avg_day_amt,'0') as avg_day_amt,
coalesce(b.avg_day_cnt,'0') as avg_day_cnt
from(	
select distinct substr(date_value,1,7) as trx_mon
from date_table
where date_value between '2023-01-01' AND '2024-06-30'
	) as a
left join
(
select 
substr(a.trx_time,1,7) as trx_mon,
last_day(max(a.trx_time)) as last_day,
day(last_day(max(a.trx_time))) as day_of_mon,
sum(a.trx_amt) as trx_amt,
count(1) as trx_cnt,
sum(a.trx_amt)/day(last_day(max(trx_time)) ) as avg_day_amt,
count(1)/day(last_day(max(trx_time)) ) as avg_day_cnt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520' and date(a.trx_time) BETWEEN '2023-01-01' AND '2024-6-30'
and ((b.mch_typ='休闲娱乐'or b.mch_typ is null)
and a.trx_amt>=288
and hour(a.trx_time) in (23,0,1,2))
group by substr(a.trx_time,1,7)
order by 1 asc) as b
on a.trx_mon = b.trx_mon
2025-05-03 时间日期(4)阶段综合-按月统计日花费,一天都不要浪费 
select 
a.trx_mon,
coalesce(b.last_day,'1900-01-01') as last_day,
coalesce(b.day_of_mon,'0') as last_day,
coalesce(b.trx_amt,'0') as trx_amt,
coalesce(b.trx_cnt,'0') as trx_cnt,
coalesce(b.avg_day_amt,'0') as avg_day_amt,
coalesce(b.avg_day_cnt,'0') as avg_day_cnt
from(	
select distinct substr(date_value,1,7) as trx_mon
from date_table
where date_value between '2023-01-01' AND '2024-06-30'
	) as a
left join
(
select 
substr(a.trx_time,1,7) as trx_mon,
last_day(max(a.trx_time)) as last_day,
day(last_day(max(a.trx_time))) as day_of_mon,
sum(a.trx_amt) as trx_amt,
count(1) as trx_cnt,
sum(a.trx_amt)/day(last_day(max(trx_time)) ) as avg_day_amt,
count(1)/day(last_day(max(trx_time)) ) as avg_day_cnt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520' and date(a.trx_time) BETWEEN '2023-01-01' AND '2024-6-30'
and ((b.mch_typ='休闲娱乐'or b.mch_typ is null)
and a.trx_amt>=288
and hour(a.trx_time) in (23,0,1,2))
group by substr(a.trx_time,1,7)
order by 1 asc) as b
on a.trx_mon = b.trx_mon
2025-05-03 时间日期(3)按月统计日花费,一天都不要浪费 
select 
substr(a.trx_time,1,7) as trx_mon,
last_day(max(a.trx_time)) as last_day,
day(last_day(max(a.trx_time))) as day,
sum(a.trx_amt) as trx_amt,
count(1) as trx_cnt,
sum(a.trx_amt)/day(last_day(max(trx_time)) ) as avg_day_amt,
count(1)/day(last_day(max(trx_time)) ) as avg_day_cnt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520' and year(a.trx_time) in (2023,2024)
and b.mch_typ='休闲娱乐'
group by substr(a.trx_time,1,7)
order by 1 asc
2025-05-03 时间日期(2)按月统计日花费,一天都不要浪费 
select 
substr(a.trx_time,1,7) as trx_mon,
last_day(max(a.trx_time)) as last_day,
day(last_day(max(a.trx_time))) as day
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520' and year(a.trx_time) in (2023,2024)
and b.mch_typ='休闲娱乐'
group by substr(a.trx_time,1,7)
order by 1 asc
2025-05-03 时间日期(2)按月统计日花费,一天都不要浪费 
select 
substr(trx_time,1,7) as trx_mon,
last_day(max(trx_time)) as last_day,
day(last_day(max(trx_time))) as day
from cmb_usr_trx_rcd 
where usr_id = '5201314520' and year(trx_time) in (2023,2024)
group by substr(trx_time,1,7)
order by 1 asc
2025-05-03 时间日期(1)按月统计日花费,一天都不要浪费 
select substr(trx_time,1,7) as trx_mon,max(last_day(trx_time)) as last_day
from cmb_usr_trx_rcd
where year(trx_time) in (2023,2024)
and usr_id = '5201314520'
group by substr(trx_time,1,7)
order by 1 asc
2025-05-03 时间日期(1)按月统计日花费,一天都不要浪费 
select substr(trx_time,1,7) as trx_mon,last_day(trx_time) as last_day
from cmb_usr_trx_rcd
where year(trx_time) in (2023,2024)
and usr_id = '5201314520'
order by 1 asc
2025-05-03 时间日期(1)按月统计日花费,一天都不要浪费 
select substr(trx_time,1,7) as trx_mon,last_day(trx_time) as last_day
from cmb_usr_trx_rcd
where year(trx_time) in (2023,2024)
and usr_id = '5201314520'
2025-05-03 时间日期(1)按月统计日花费,一天都不要浪费 
select month(trx_time) as trx_mon,last_day(trx_time) as last_day
from cmb_usr_trx_rcd
where year(trx_time) in (2023,2024)
and usr_id = '5201314520'
2025-05-03 表连接(5)哪些没被分出来,用左用内你来猜 
select b.mch_typ,a.mch_nm,count(1) as trx_cnt,sum(a.trx_amt) as trx_amt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm 
where year(a.trx_time) = 2024 and a.usr_id = '5201314520'
and b.mch_nm is null 
group by b.mch_typ,a.mch_nm
2025-05-03 表连接(4)渣男把钱花在哪儿,维表可以来帮忙 
select b.mch_typ,count(1) as trx_cnt ,sum(trx_amt) as trx_amt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm 
where a.usr_id = '5201314520' and year(a.trx_time) = 2024
group by b.mch_typ
2025-05-03 表连接(4)渣男把钱花在哪儿,维表可以来帮忙 
select b.mch_typ,count(1) as trx_cnt ,sum(trx_amt) as trx_amt
from cmb_usr_trx_rcd as a
left join cmb_mch_typ as b
on a.mch_nm = b.mch_nm 
where a.usr_id = '5201314520'
group by b.mch_typ
2025-05-03 表连接(3)一直使用一张表,现在开始两张表 
select mch_typ,count(1) as total_mch,count(distinct mch_nm) as unique_mch_cnt
from cmb_mch_typ
group by mch_typ