File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
coding-challange/leetcode/easy/1777-products-price-for-each-store Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ # 1777-products-price-for-each-store
2+ # leetcode/easy/1777. Product's Price for Each Store
3+ # Difficulty: easy
4+ # URL: https://leetcode.com/problems/products-price-for-each-store/
5+ #
6+ # Input:
7+ # Products table:
8+ # +-------------+--------+-------+
9+ # | product_id | store | price |
10+ # +-------------+--------+-------+
11+ # | 0 | store1 | 95 |
12+ # | 0 | store3 | 105 |
13+ # | 0 | store2 | 100 |
14+ # | 1 | store1 | 70 |
15+ # | 1 | store3 | 80 |
16+ # +-------------+--------+-------+
17+ # Output:
18+ # +-------------+--------+--------+--------+
19+ # | product_id | store1 | store2 | store3 |
20+ # +-------------+--------+--------+--------+
21+ # | 0 | 95 | 100 | 105 |
22+ # | 1 | 70 | null | 80 |
23+ # +-------------+--------+--------+--------+
24+ # Explanation:
25+ # Product 0 price's are 95 for store1, 100 for store2 and, 105 for store3.
26+ # Product 1 price's are 70 for store1, 80 for store3 and, it's not sold in store2.
27+
28+
29+ # NOTE: best practice
30+ SELECT product_id,
31+ MAX (CASE WHEN store = ' store1' THEN price END) AS store1,
32+ MAX (CASE WHEN store = ' store2' THEN price END) AS store2,
33+ MAX (CASE WHEN store = ' store3' THEN price END) AS store3
34+ FROM Products
35+ GROUP BY product_id;
36+
37+
38+ # NOTE: naive solution
39+ SELECT P .product_id ,
40+ (SELECT price
41+ FROM Products
42+ WHERE store = ' store1'
43+ AND product_id = P .product_id )
44+ AS store1,
45+ (SELECT price
46+ FROM Products
47+ WHERE store = ' store2'
48+ AND product_id = P .product_id )
49+ AS store2,
50+ (SELECT price
51+ FROM Products
52+ WHERE store = ' store3'
53+ AND product_id = P .product_id )
54+ AS store3
55+ FROM Products AS P
56+ GROUP BY P .product_id ;
You can’t perform that action at this time.
0 commit comments