Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet code] 1164. Product Price at a Given Date

์ธ” 2023. 7. 17. 22:21


๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ” ๋ฌธ์ œ

https://leetcode.com/problems/product-price-at-a-given-date/

 

Product Price at a Given Date - LeetCode

Can you solve this real interview question? Product Price at a Given Date - Table: Products +---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | new_price | int | | change_date | date | +---------------+----

leetcode.com

Table : Products

Write an SQL query to find the prices of all products on 2019-08-16. Assume the price of all products before any change is 10.

Return the result table in any order.

The query result format is in the following example.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

2019.08.16 ๊ธฐ์ค€ ๋ชจ๋“  ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜๋Š”๋ฐ

๋ฐ์ดํ„ฐ๋Š” change_date์— ์ œํ’ˆ๋ณ„ ๊ฐ€๊ฒฉ์ด ์ƒˆ๋กญ๊ฒŒ ๋ณ€๊ฒฝ๋์Œ์„ ์•Œ๋ ค์ค€๋‹ค.

2019.08.16 ๊นŒ์ง€ ๋ณ€ํ™”๊ฐ€ ์—†์—ˆ๋˜ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์€ 10์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ์ตœ์‹  ์ผ์ž์˜ ๋ณ€๊ฒฝ ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

2019.08.16 ๊ธฐ์ค€ ์ƒํ’ˆ ์•„์ด๋””๋ณ„ ๊ฐ€์žฅ ์ตœ๊ทผ ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ์ผ์ž์˜ ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ1๊ณผ

2019.08.16๊นŒ์ง€ ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ์ด ์—†์—ˆ๋˜ ์ƒํ’ˆ์˜ ๊ฒฝ์šฐ(์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์•„์ด๋”” ๋น„๊ต) new_price ์ปฌ๋Ÿผ์— ๊ฐ’์„ 10์œผ๋กœ ์ง€์ •ํ•ด์„œ union์œผ๋กœ ์ง‘ํ•ฉ์ฒ˜๋ฆฌ ํ•ด์ค€๋‹ค.

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')

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leet code] 619. Biggest Single Number  (0) 2023.09.02
[Leet code] 1173. Immediate Food Delivery I  (0) 2023.08.03
[LeetCode] 1321. Restaurant Growth  (3) 2023.06.17
[LeetCode] 1158. Market Analysis I  (0) 2023.06.08
[LeetCode] 1070. Product Sales Analysis III  (0) 2023.05.22