LeetCode-1164. Specified date price of the product (medium) UNION

Product Data Sheet: Products

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| product_id | int |
| new_price | int |
| CHANGE_DATE | DATE |
+ --------------- + -------- - +
primary key of this table is (product_id, change_date).
Each row of this table are recording a new price of a product after a certain date change.
 

Write a SQL to find the price of all products at 2019-08-16, assuming that all products prices before the modification is 10.

Results illustrated in the following format:

Products table:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1          | 20        | 2019-08-14  |
| 2          | 50        | 2019-08-14  |
| 1          | 30        | 2019-08-15  |
| 1          | 35        | 2019-08-16  |
| 2          | 65        | 2019-08-17  |
| 3          | 20        | 2019-08-18  |
+------------+-----------+-------------+

Result table:
+------------+-------+
| product_id | price |
+------------+-------+
| 2          | 50    |
| 1          | 35    |
| 3          | 10    |
+------------+-------+

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/product-price-at-a-given-date
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation: Write a SQL to look at  2019-08-16  price of all products, it is assumed that all products before the price modifications are  10.

Thinking:

Problem solving:

This question of the difficulty is that we need to deal with two situations:

For 2019-08-16 day before and changed the price of the product, just need to find the last modification to the price
for the before and have not changed the price of products 2019-08-16 day, we need to return to the initial price of 10
a more natural idea is to calculate the results of the two cases, then combined using the UNION, according to the final price ordering

In the first case, we have:

SELECT product_id, new_price AS price
FROM Products
WHERE (product_id, change_date) IN (SELECT product_id, MAX(change_date)
                                    FROM Products
                                    WHERE change_date <= '2019-08-16'
                                    GROUP BY product_id)


In the second case, we have:

SELECT DISTINCT product_Id, 10 AS price
FROM Products
WHERE product_id NOT IN (SELECT product_id FROM Products WHERE change_date <= '2019-08-16')


Finally, the two cases were combined and sorted according to price:

SELECT * 
FROM 
(SELECT product_id, new_price AS price
 FROM Products
 WHERE (product_id, change_date) IN (
                                     SELECT product_id, MAX(change_date)
                                     FROM Products
                                     WHERE change_date <= '2019-08-16'
                                     GROUP BY product_id)

 UNION

 SELECT DISTINCT product_id, 10 AS price
 FROM Products
 WHERE product_id NOT IN (SELECT product_id FROM Products WHERE change_date <= '2019-08-16')
) tmp
ORDER BY price DESC

Knowledge points:

Published 118 original articles · won praise 2 · Views 4825

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104611645