当前位置:网站首页>Li Kou 10821084 solution_ Question of SQL query type

Li Kou 10821084 solution_ Question of SQL query type

2022-06-10 20:35:00 Across the mountains and rivers, across the sea

Their thinking

as Take the alias
join on Table joins
group by grouping
having Conditions

Code

1084. Sales analysis III

# Write your MySQL query statement below
/* Write a SQL Inquire about , The report 2019 Products sold only in the spring of . Only in 2019-01-01 to 2019-03-31( contain ) Goods sold between .  With   In any order   Return result table */
select s.product_id,p.product_name
from sales as s
join product as p
on s.product_id=p.product_id/* Join the two tables */
group by s.product_id
having min(s.sale_date) >= '2019-01-01' and
       max(s.sale_date) <= '2019-03-31'

Their thinking

To find the best seller, If there is a similar level , Then we need to find them all .
The corresponding in the table seller Per row price The sum is the sales amount => take seller_id Conduct group by Group summation .
After getting every seller After total sales , In descending order , use limit 1 Take the first place => This will bring about a problem : If there is a tie , Can't take it out , The solution is : First use a subquery to get the best seller Sales volume , Then you can find that the sales amount is equal to this seller that will do .

  1. Sales analysis I
select seller_id
from sales
group by seller_id
having sum(price)=(select sum(price) as pr
								from sales
								group by seller_id
								order by pr desc
								limit 1)
原网站

版权声明
本文为[Across the mountains and rivers, across the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101928157746.html