Snoopy [리트코드/SQL] 12. Rearrange Products Table (1795)
코딩테스트/리트코드 SQL

[리트코드/SQL] 12. Rearrange Products Table (1795)

Sooyoon Jeong 2022. 12. 16.

🤔 문제 분석

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

 

댓글