1082. Sales Analysis I 难度:简单

1、题目描述

Write an SQL query that reports the best seller by total sales price, If there is a tie, report them all.
The query result format is in the following example:

Product table:

product_idproduct_nameunit_price
1S81000
2G4800
3iPhone1400

Sales table:

seller_idproduct_idbuyer_idsale_datequantityprice
1112019-01-2122000
1222019-02-171800
2232019-06-021800
3342019-05-1322800

Result table:

seller_id
1
3

Both sellers with id 1 and 3 sold products with the most total price of 2800.

来源:力扣(LeetCode)

2、解题思路

其实和上一题都是差不多的,这次换一种解法
1# 首先,对每个销售员进行出售价格求和,然后倒序

select seller_id ,sum(price) as p
from Sales
group by seller_id
order by price desc

2# 然后,增加一列排名列 参考 分数排名
select seller_id ,p,@j:=@j+(@i<>(@i:=p)) as rank
3# 最后,就是找出rank=1的记录

3、提交记录

select seller_id
from(
select  seller_id ,p,@j:=@j+(@i<>(@i:=p)) as rank
from(
select seller_id ,sum(price) as p
from Sales
group by seller_id
order by price desc)b,(select @i:=0,@j:=0)a)c
where rank=1
order by seller_id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值