-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1795. Rearrange Products Table.sql
More file actions
48 lines (38 loc) · 1.23 KB
/
1795. Rearrange Products Table.sql
File metadata and controls
48 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Question 1795. Rearrange Products Table
Link: https://leetcode.com/problems/rearrange-products-table/description/?envType=problem-list-v2&envId=database
Table: Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| store1 | int |
| store2 | int |
| store3 | int |
+-------------+---------+
product_id is the primary key (column with unique values) for this table.
Each row in this table indicates the product's price in 3 different stores: store1, store2, and store3.
If the product is not available in a store, the price will be null in that store's column.
Write a solution to rearrange the Products table so that each row has (product_id, store, price). If a product is not available in a store, do not include a row with that product_id and store combination in the result table.
Return the result table in any order.
*/
SELECT
product_id,
'store1' AS store,
store1 AS price
FROM Products
WHERE store1 IS NOT NULL
UNION ALL
SELECT
product_id,
'store2' AS store,
store2 AS price
FROM Products
WHERE store2 IS NOT NULL
UNION ALL
SELECT
product_id,
'store3' AS store,
store3 AS price
FROM Products
WHERE store3 IS NOT NULL