-- rollup方法(mysql8.0+)
with t1 as (
select date_format(trx_time,'%Y-%m') as trx_mon,mch_nm,sum(trx_amt) as sum_trx_amt
from cmb_usr_trx_rcd
where trx_time>='2024-01-01' and trx_time<'2025-01-01' and usr_id=5201314520
group by mch_nm,trx_mon
with rollup
having trx_mon is not null or mch_nm is not null
),
t2 as (
select coalesce(trx_mon,2024) as trx_mon,mch_nm,sum_trx_amt,row_number() over(partition by coalesce(trx_mon,2024) order by sum_trx_amt desc) as rk
from t1
)
select trx_mon,mch_nm,sum_trx_amt
from t2
where rk<=3
order by trx_mon asc,sum_trx_amt desc
select user_id,dayname(start_time) as day_of_week,count(*) as listens_per_day
from listen_rcd
group by user_id,day_of_week
order by user_id,day_of_week
select *
from cmb_usr_trx_rcd
where (trx_time>='2024-06-08' and trx_time<'2024-06-11' or trx_time>='2024-09-15' and trx_time<'2024-09-18')and usr_id=5201314520
order by trx_time
select *
from cmb_usr_trx_rcd
where trx_time>='2024-06-08' and trx_time<'2024-06-11' or trx_time>='2024-09-15' and trx_time<'2024-09-18'and usr_id=5201314520
order by trx_time
select *
from cmb_usr_trx_rcd
where trx_time>='2024-06-08' and trx_time<'2024-06-11' or trx_time>='2024-09-15' and trx_time<'2024-09-18'and usr_id='5201314520'
order by trx_time
select count(distinct case when time(login_time) between '07:30:00' and '09:30:00' or time(login_time) between '18:30:00' and '20:30:00' then usr_id end) as commute,
count(distinct case when time(login_time) between '11:30:00' and '14:00:00' then usr_id end) as lunch_break,
count(distinct case when time(login_time) between '22:30:00' and '23:59:59' or time(login_time) between '00:00:00' and '01:00:00' then usr_id end) as bedtime
from user_login_log
where login_time>=date_format(date_sub(curdate(),interval 1 month),'%Y-%m-01') and login_time<date_format(curdate(),'%Y-%m-01')
select count(distinct usr_id) as active_users
from user_login_log
where login_time>=date_format(date_sub(curdate(),interval 1 month),'%Y-%m-01') and login_time<date_format(curdate(),'%Y-%m-01')
select year(dt) as Y,
cast(avg(case when city='beijing' then tmp_h end) as decimal(4,2)) as 北京,
cast(avg(case when city='shanghai' then tmp_h end) as decimal(4,2)) as 上海,
cast(avg(case when city='shenzhen' then tmp_h end) as decimal(4,2)) as 深圳,
cast(avg(case when city='guangzhou' then tmp_h end) as decimal(4,2)) as 广州
from weather_rcd_china
where year(dt) between 2011 and 2022
group by year(dt)
select year(dt) as Y,
round(avg(case when city='beijing' then tmp_h end),2) as 北京,
round(avg(case when city='shanghai' then tmp_h end),2) as 上海,
round(avg(case when city='shenzhen' then tmp_h end),2) as 深圳,
round(avg(case when city='guangzhou' then tmp_h end),2) as 广州
from weather_rcd_china
where year(dt) between 2011 and 2022
group by year(dt)
select city,count(case when con like '%雪%' then 1 end) as snowy_days
from weather_rcd_china
where month(dt) in (12,1,2)
group by city
order by snowy_days desc
select trx_amt,count(*) as total_trx_cnt,count(distinct usr_id) as unique_usr_cnt,
round(count(*)/count(distinct usr_id),4) as avg_trx_per_user
from cmb_usr_trx_rcd
where mch_nm='红玫瑰按摩保健休闲' and trx_time>='2023-01-01' and trx_time<'2024-07-01'
group by trx_amt
order by avg_trx_per_user desc
limit 5
select trx_amt,count(*) as trx_cnt
from cmb_usr_trx_rcd
where mch_nm='红玫瑰按摩保健休闲' and year(trx_time)=2024 and month(trx_time) between 1 and 7
group by trx_amt
order by trx_cnt desc
limit 5
select date(trx_time) as trx_date,max(trx_amt) as max_trx_amt,min(trx_amt) as min_trx_amt,
avg(trx_amt) as avg_trx_amt,sum(trx_amt) as sum_trx_amt
from cmb_usr_trx_rcd
where trx_time>='2024-09-01' and trx_time<'2024-10-01' and mch_nm='红玫瑰按摩保健休闲'
group by date(trx_time)
order by trx_date
select 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
select *
from cmb_usr_trx_rcd
where usr_id=5201314520 and trx_time>='2024-09-01' and trx_time<'2024-10-01' 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 trx_time>='2024-09-01' and trx_time<'2024-10-01' and hour(trx_time) between 1 and 5
order by trx_time