문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
Table: Products
| Column Name | Type |
|---|---|
| product_id | int |
| store1 | int |
| store2 | int |
| store3 | int |
product_id는 이 테이블의 기본 키이다.
각 행은 3개의 다른 상점 store1, store2, store에 제품의 가격을 나타낸다.
상점에 제품이 없다면 가격은 상점 컬럼에 null이 될 것이다.
Prodcut 테이블을 재구성해서 각 행이 (product_id, store, pridce)를 갖도록 하는 방법을 작성해라. 특정 매장에 제품이 없는 경우, 해당 product_id와 store 조합에 대한 행은 결과 테이블에 포함하지 않는다.
Input:
Products table:
| product_id | store1 | store2 | store3 |
|---|---|---|---|
| 0 | 95 | 100 | 105 |
| 1 | 70 | null | 80 |
Output:
| product_id | store | pridct |
|---|---|---|
| 0 | store1 | 95 |
| 0 | store2 | 100 |
| 0 | store3 | 105 |
| 1 | store1 | 70 |
| 1 | store3 | 80 |
Explanation:
제품 0은 모든 세 상점에서 각 95, 100, 105로 가능하다.
제품 1은 store1에서 70, store3에서 80으로 가능하다. 제품은 store2에서 불가능하다.
-- Write your PostgreSQL query statement below
select product_id, store, price
from Products, lateral(values ('store1', store1), ('store2', store2), ('store3', store3)) as v(store, price)
where price is not null