본문 바로가기
코딩테스트/SQL

[LeetCode][MySQL] Rearrange Products Table

by 포뇨j 2023. 12. 29.

https://leetcode.com/problems/rearrange-products-table

 

Rearrange Products Table - LeetCode

Can you solve this real interview question? Rearrange Products Table - Table: Products +-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | store1 | int | | store2 | int | | store3 | int | +-------------+-------

leetcode.com

 

 

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

 

UNION을 이용해서 각 store의 행을 합친 결과를 추출했다.