select
lpad(hour(enter_time),2,'0') as hour_entered,
count(1) as enter_count
from
ks_live_t1 t1 join ks_live_t2 t2 on t1.live_id = t2.live_id
group by
hour_entered
order by
hour_entered asc
菜鸟啥也不懂,是不是可以不用子查询
讲道理这里能用union 么,我用了之后运行出错,最后还是参照答案写
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) >= '22'
or hour(trx_time) <= '5'
)
order by
trx_time
先查出前十名发现不对,看答案实际是取小于10的,最后使用子查询搞定,参考答案的定义方法不会用
select
t.student_id,
t.name,
t.score,
t.ranking
from(
select
s1.student_id,
s1.name,
s2.score,
(rank() over(order by s2.score desc)) as ranking
from
students s1
join scores s2 on s1.student_id = s2.student_id
where
s1.grade_code = 'S1' and subject = '物理'
) t
where
t.ranking <= 10
order by
ranking;
这题坑很多。我应该再多出2题的。这样就能把四种情况都给大家搞清楚。
1.order by score limit 10,不带窗口函数
2\3\4、三种排序窗口函数。
你先自己试试吧
select
city,
sum(case when con like '%雪%' then 1 else 0 end) as snowy_days
from
weather_rcd_china
where
month(dt) in (12,1,2)
group by
city
order by
2 desc
select
city,
sum(case when con like '%多云%' then 1 else 0 end) as cloudy_days,
concat(cast(sum(case when con like '%多云%' then 1 else 0 end) / count(*)*100 as decimal(10,2)),'%') as p
from
weather_rcd_china
where
year(dt) = 2021
group by
city
order by
3 desc
select
t2.live_id,
t2.live_nm,
count(t1.usr_id) as enter_count
from
ks_live_t1 t1
join ks_live_t2 t2
on t1.live_id = t2.live_id
where
date_format(t1.enter_time, '%Y-%m-%d %H') = '2021-09-12 23'
group by
t1.live_id, t2.live_nm
order by
enter_count desc
limit 5;
select
lpad(hour(enter_time),2,'0') as hour_entered,
count(1) as enter_count
from
ks_live_t1 t1 join ks_live_t2 t2 on t1.live_id = t2.live_id
group by
hour_entered
order by
hour_entered
select
hour(enter_time) as hour_entered,
count(1) as enter_count
from
ks_live_t1 t1 join ks_live_t2 t2 on t1.live_id = t2.live_id
group by
hour_entered
order by
hour_entered
select
hour(enter_time) as hour_entered,
count(1) as enter_count
from
ks_live_t1 t1 join ks_live_t2 t2 on t1.live_id = t2.live_id
group by
hour(enter_time)
order by
hour_entered
select
sum(case when right(card1,1) = right(card2,1) then 1 else 0 end)/2 as cnt,
count(1)/2 as ttl_cnt,
cast(sum(case when right(card1,1)=right(card2,1) then 1 else 0 end)/count(1) AS DECIMAL(4,3)) as p
from hand_permutations
select
sum(case when left(card1,1) = left(card2,1) then 1 else 0 end)/2 as cnt,
count(1)/2 as ttl_cnt,
cast(sum(case when right(card1,1)=right(card2,1) then 1 else 0 end)/count(1) AS DECIMAL(4,3)) as p
from hand_permutations
select
sum(case when left(card1,1) = left(card2,1) then 1 else 0 end)/2 as cnt,
count(1)/2 as ttl_cnt,
cast(sum(case when left(card1,1) = left(card2,1) then 1 else 0 end)/count(1) as decimal(4,3)) as p
from hand_permutations
select
sum(case when left(card1,1) = left(card2,1) then 1 else 0 end)/2 as cnt,
count(1)/2 as ttl_cnt,
(sum(case when left(card1,1) = left(card2,1) then 1 else 0 end)/2)/(count(1)/2) as p
from hand_permutations