LeetCode-1082. 销售分析 I (简单)

产品表:Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
product_id 是这个表的主键.
销售表:Sales

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+------ ------+---------+
这个表没有主键,它可以有重复的行.
product_id 是 Product 表的外键.
 

编写一个 SQL 查询,查询总销售额最高的销售者,如果有并列的,就都展示出来。

查询结果格式如下所示:

Product 表:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+

Sales 表:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 2          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 4        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+

Result 表:
+-------------+
| seller_id   |
+-------------+
| 1           |
| 3           |
+-------------+
Id 为 1 和 3 的销售者,销售总金额都为最高的 2800。
通过次数1,698提交次数2,316

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sales-analysis-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

审题:查询销售额最高的消费者,需要展示出并列的来。

思考:有些消费者消费了几个产品,需要按照销售员id分组,求和,排序。

解题:

解法一

先找出每个卖家的总售价。对卖家分组,求和。

-- 练习
select seller_id,sum(price) as `price` from sales group by seller_id;


select seller_id,sum(price) as `price`
from sales
group by seller_id

再最高的总售价。

select max(A.price) as `max_price`
from
(
	select seller_id,sum(price) as `price`
	from sales
	group by seller_id
) as A

或者用limit取最大值

-- 练习

select seller_id,sum(price) as `price`
	from sales
	group by seller_id
    order by price dese limit 0,1;


select seller_id,sum(price) as `price`
	from sales
	group by seller_id
        order by price desc
        limit 0,1

再找出总售价与最高售价相等的卖家。连接表A与卖家总售价表。

select C.seller_id
from
(
    select max(A.price) as `max_price`
    from
    (
        select seller_id,sum(price) as `price`
        from sales
        group by seller_id
    ) as A
) as B
join (
    select seller_id,sum(price) as `price`
    from sales
    group by seller_id
) as C
    on(B.max_price = C.price)

select C.seller_id
from
(
	select seller_id,sum(price) as `price`
	from sales
	group by seller_id
   order by price desc
   limit 0,1
) as B
join (
    select seller_id,sum(price) as `price`
    from sales
    group by seller_id
) as C
    on(B.price = C.price)

解法二

求与最大值相等的问题。

第一种采用解法一的思路,先求出最大值,再表与最大值连接,选出等于最大值的行。

第二种对每行数据,判断其与最大值是否相等。每次判断时都会计算一次最大值。

求最高售价同解法一。

	SELECT SUM(price) AS `price`
	FROM sales
	GROUP BY seller_id
	ORDER BY price DESC
	LIMIT 0,1

每个卖家都检查一次与最高售价是否相等。

SELECT S.seller_id
FROM sales AS S
GROUP BY S.seller_id
HAVING SUM(S.price) = (
	SELECT SUM(price) AS `price`
	FROM sales
	GROUP BY seller_id
	ORDER BY price DESC
	LIMIT 0,1
)

 

知识点:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值