赞
踩
SELECT
t1.id,
t1.name,
t1.score
FROM (SELECT grade.id,
grade.language_id,
language.name,
grade.score,
DENSE_RANK() over (PARTITION BY grade.language_id ORDER BY grade.score desc) as rank
FROM grade
JOIN language on language.id = grade.language_id) t1
where t1.rank in (1,2)
ORDER BY t1.NAME,t1.SCORE DESC,t1.ID;
ROW_NUMBER():ROW_NUMBER()函数作用就是将select查询到的数据进行排序,每一条数据加一个序号,他不能用做于学生成绩的排名,一般多用于分页查询
RANK()函数:顾名思义排名函数,可以对某一个字段进行排名。row_number()并不会进行并列排序,而rank()函数会;
DENSE_RANK()函数:是排名函数,和RANK()功能相似,也是对字段进行排名;但是二者的区别如下图:
NTILE()函数:将有序分区中的行分发到指定数目的组中,各个组有编号,编号从1开始,就像我们说的’分区’一样 ,分为几个区,一个区会有多少个
SELECT
t1.date,
sum(case when t1.rank = 1 then 1 else 0 end)
FROM (SELECT id,
user_id,
client_id,
date,
row_number() over (PARTITION BY user_id order by date) as rank
from login) t1
group by date;
select
user_id,
min(date) as first_buy_date,
count(user_id)
from order_info
where status = 'completed' and date > '2025-10-15'
and (product_name = 'Python' or product_name = 'C++' or product_name = 'Java')
group by user_id
having count(user_id) >= 2
order by user_id;
group by可以对应两个聚合函数,后面跟随having来代替where
select
s1.emp_no,
s1.salary,
sum(s2.salary)
from salaries s1, salaries s2
where s1.to_date='9999-01-01' and s2.to_date='9999-01-01' and s1.emp_no >= s2.emp_no
group by s1.emp_no
order by s1.emp_no;
使用两个相同的表格对同一个字段进行对比来反映该字段值的位置
select round(count(distinct l1.user_id)*1.0/(select count(distinct user_id) from login),3)
from login l1, login l2
where l1.user_id = l2.user_id and l2.date = date(l1.date,'+1 day')
哎 很难理解呀 还在理解
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。