select
usr_id,
mch_nm,
sum(trx_amt) as trx_amt,
count(trx_amt) as trx_cnt,
min(trx_time) as first_time
from
cmb_usr_trx_rcd
where
usr_id='5201314520'
and
trx_amt >= 288
group by mch_nm
order by trx_cnt desc
为什么答案还有group by usr_id
select
mch_nm
,sum(trx_amt) as trx_amt
,count(1) as trx_cnt
,min(trx_time) as first_time
from
cmb_usr_trx_rcd
where
usr_id='5201314520'
and trx_amt>=288
group by mch_nm
order by 3 desc
1、感受多列分组与单列分组,仔细对比这段代码跟你的代码的区别,数字是一样的;
2、以后你取数了,业务方让你取特定商户、特定分类的聚合数据,也可以加上,这样比较【踏实】(虽然第一列是重复的)
select
*
from
cmb_usr_trx_rcd
where
usr_id='5201314520'
and
date(trx_time) between '2024-09-01' and '2024-09-30'
and time(trx_time)>= '01:00:00'
and time(trx_time)< '06:00:00'
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 (1,2,3,4,5)
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 1 and 5
表达的意思都一样吗
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
usr_id,
mch_nm,
sum(trx_amt) as trx_amt,
count(trx_amt) as trx_cnt,
min(trx_time) as first_time
from
cmb_usr_trx_rcd
where
usr_id='5201314520'
and
trx_amt >= 288
group by mch_nm
order by trx_cnt desc
select
mch_nm,
sum(trx_amt) as sum_trx_amt
from
cmb_usr_trx_rcd
where
usr_id='5201314520'
and
trx_time between '2024-01-01' and '2024-12-31'
group by mch_nm
order by sum_trx_amt
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
select
*
from
scores
where
exam_date='2024-06-30'
and
((subject='历史' and score >= 90)
or(subject='政治' and score >= 90)
or (subject='地理' and score >= 90))
order by score desc ,student_id,subject
select
*
from
scores
where
exam_date='2024-06-30'
and
((subject='历史' and score >= 90)
or(subject='地理' and score >= 90)
or (subject='历史' and score >= 90))
order by score desc ,student_id,subject
select
*
from
scores
where
exam_date='2024-06-30'
and
((subject='历史' and score >=90)
or(subject='地理' and score >=90)
or (subject='历史' and score >=90))
order by score desc ,student_id,subject
select
*
from
scores
where
exam_date='2024-06-30'
and
((subject='历史' and score >=90)
or
(subject='地理' and score >=90)
or
(subject='历史' and score >=90))
order by score desc ,student_id,subject