当前位置:网站首页>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
边栏推荐
猜你喜欢

【天线】【2】一些名词和简单概念的解释,仍然

RMAN备份概念_关于备份集(Backup Set)

汇编_基础概念

85.(leaflet之家)leaflet军事标绘-直线箭头绘制

Blow up the idea artifact in use recently

EDA open source simulation tool verilator getting started 1: installation and testing

Modify PE program entry point

RedLock红锁安全性争论(上)

自制编译器学习1:Cb编译器的使用

Out object of JSP development details you should know (I)
随机推荐
2022-2028 global LED ribbon industry research and trend analysis report
GC study notes
2022-2028 global anti UAV (c-uas) technology industry research and trend analysis report
HJ22 汽水瓶
Nacos二次开发
OpenInfra Summit 2022 | 华云数据用户再度入围超级用户大奖
Apple wins judge dismisses iPhone Security Fraud Class Action
Market Research - current market situation and future development trend of functional paper additives in the world and China
extends和super的使用
Alibaba cloud ack pull free enterprise ACR image
Puzzle (105) plane inversion
Compilation_ Basic concepts
SQL: 重新格式化部门表 (行转列问题:Group by + 聚合函数)
[introduction to the paper] 2204 Vqgan-clip (open source):open domain image generation and editing with natural language guidance
. Net C # Foundation (6): namespace - a sharp tool for organizing code
Self made compiler learning 3: introduction to flex and bison
Implementation of WTM based on NETCORE framework
js类的创建和constructor的构造函数的使用
mysql中if和ifnull
Quarkus practical learning I