🤔 문제 분석
1) Write an SQL query to rearrange the Products table so that each row has (product_id, store, price).
2) 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.
3) Return the result table in any order.
💡 풀이
SELECT product_id, 'store1' as store, store1 AS price
FROM products
WHERE store1 IS NOT NULL
UNION
SELECT product_id, 'store2' as store, store2 AS price
FROM products
WHERE store2 IS NOT NULL
UNION
SELECT product_id, 'store3' as store, store3 AS price
FROM products
WHERE store3 IS NOT NULL
'코딩테스트 > 리트코드 SQL' 카테고리의 다른 글
[리트코드/SQL] 14. Second Highest Salary (176) (0) | 2022.12.16 |
---|---|
[리트코드/SQL] 13. Tree node (608) (0) | 2022.12.16 |
[리트코드/SQL] 11. Employees With Missing Information (1965) (0) | 2022.12.16 |
[리트코드/SQL] 10. Patients With a Condition (1527) (0) | 2022.12.08 |
[리트코드/SQL] 9. Group Sold Products By The Date (1484) (0) | 2022.12.08 |
댓글