You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
select
customer_number
from orders group by customer_number
order bycount(customer_number) desclimit1;
解析
方法一只能查出一位订单数最多的客户
通过 group by 对 customer_number 进行分组
使用 count 计算出每个顾客的订单数,按照倒序进行排序
因为是倒序排序,所以使用 limit 取第一条数据
limit offset length 第一个数是偏移量,第二个数是要取的长度
limit 1 表示从第一条数据开始取一条
SQL:方法二
select customer_number from (
select
customer_number,
dense_rank() over(order bycount(*) desc) as rn
from orders group by customer_number
) temp where rn =1;
解析
方法二是进阶,查询出多位顾客订单数并列最多
使用 group by 对 customer_number 进行分组
使用 count() 计算出每个顾客的订单数,倒序排列
使用窗口函数 dense_rank() 按照 每个顾客的订单数进行排名,
将其作为临时表,查询这个临时表,筛选出排名第一的顾客
The text was updated successfully, but these errors were encountered:
题目
查询出最多订单的客户
customer_number
。进阶:如果有多位顾客订单数并列最多,你能找到他们所有的
customer_number
吗?分析
通过
分组
和倒序排序
将订单最多的客户放到最上面,在通过limit
将第一条数据拿出来。SQL:方法一
解析
方法一只能查出一位订单数最多的客户
group by
对customer_number
进行分组count
计算出每个顾客的订单数,按照倒序进行排序limit
取第一条数据limit offset length
第一个数是偏移量,第二个数是要取的长度limit 1
表示从第一条数据开始取一条SQL:方法二
解析
方法二是进阶,查询出多位顾客订单数并列最多
group by
对customer_number
进行分组count()
计算出每个顾客的订单数,倒序排列dense_rank()
按照每个顾客的订单数进行排名
,The text was updated successfully, but these errors were encountered: