当前位置:网站首页>LeetCode-1445. Apples and oranges

LeetCode-1445. Apples and oranges

2022-06-12 06:23:00 Border wanderer

surface : Sales

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| sale_date     | date    |
| fruit         | enum    | 
| sold_num      | int     | 
+---------------+---------+
(sale_date,fruit) Is the primary key of the table .
This table contains... In each day " Apple " and " orange " Sales of .
 

Write a SQL  Inquire about ,  Report every day   Apple   and   orange   The difference in the number of sales .

The returned result table ,  According to the format  ('YYYY-MM-DD') Of sale_date Sort .

The query result table is shown in the following example :

Sales surface :
+------------+------------+-------------+
| sale_date  | fruit      | sold_num    |
+------------+------------+-------------+
| 2020-05-01 | apples     | 10          |
| 2020-05-01 | oranges    | 8           |
| 2020-05-02 | apples     | 15          |
| 2020-05-02 | oranges    | 15          |
| 2020-05-03 | apples     | 20          |
| 2020-05-03 | oranges    | 0           |
| 2020-05-04 | apples     | 15          |
| 2020-05-04 | oranges    | 16          |
+------------+------------+-------------+

Result surface :
+------------+--------------+
| sale_date  | diff         |
+------------+--------------+
| 2020-05-01 | 2            |
| 2020-05-02 | 0            |
| 2020-05-03 | 20           |
| 2020-05-04 | -1           |
+------------+--------------+

stay 2020-05-01, sold 10 An apple and 8 An orange ( The difference is 10 - 8 = 2).
stay 2020-05-02, sold 15 An apple and 15 An orange ( The difference is 15 - 15 = 0).
stay 2020-05-03, sold 20 An apple and 0 An orange ( The difference is 20 - 0 = 20).
stay 2020-05-04, sold 15 An apple and 16 An orange ( The difference is 15 - 16 = -1).

# Write your MySQL query statement below
SELECT s1.sale_date, (s2.sold_num - s1.sold_num) as diff  
FROM Sales as s1 INNER JOIN Sales as s2 
ON (s1.fruit != s2.fruit AND s1.sale_date = s2.sale_date)
GROUP BY s1.sale_date

原网站

版权声明
本文为[Border wanderer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010609443044.html