当前位置:网站首页>Sql: Sales Analysis III

Sql: Sales Analysis III

2022-06-09 08:32:00 Jieyou grocery store Q

Table: Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
Product_id Is the primary key of the table .
 Each row of the table shows the name and price of each product .
Table: Sales

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+------ ------+---------+
 This table has no primary key , It can have duplicate lines .
product_id  yes  Product  Table foreign key .
 Each row of the table contains some information about a sale .
 

 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 .

 The query result format is as follows .

 

 Example  1:

 Input :
Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+
Sales table:
+-----------+------------+----------+------------+----------+-------+
| 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  |
+-----------+------------+----------+------------+----------+-------+
 Output :
+-------------+--------------+
| product_id  | product_name |
+-------------+--------------+
| 1           | S8           |
+-------------+--------------+
 explain :
id by 1 Of products are only available in 2019 Spring sales .
id by 2 Our products are in 2019 Spring sales , But also 2019 Sales after spring 2004 .
id 3 Our products are in 2019 Sales after spring 2004 .
 We only return products 1, Because it is 2019 Products sold only in the spring of .

Method 1

The total number of sales is equal to the number of spring

select
    a.product_id,
    b.product_name
from 
    Sales a 
    left join Product b on a.product_id = b.product_id 
group by product_id
having sum(sale_date between "2019-01-01" and "2019-03-31") = count(sale_date)

Method 2

Sales outside spring are 0 Of

select
    a.product_id,
    b.product_name
from 
    Sales a 
    left join Product b on a.product_id = b.product_id 
group by product_id
having sum(sale_date not between "2019-01-01" and "2019-03-31") = 0
原网站

版权声明
本文为[Jieyou grocery store Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090825279540.html