[LeetCode] Rearrange Products Table

아르당·2026년 7월 1일

LeetCode

목록 보기
385/602
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

Table: Products

Column NameType
product_idint
store1int
store2int
store3int

product_id는 이 테이블의 기본 키이다.
각 행은 3개의 다른 상점 store1, store2, store에 제품의 가격을 나타낸다.
상점에 제품이 없다면 가격은 상점 컬럼에 null이 될 것이다.

Prodcut 테이블을 재구성해서 각 행이 (product_id, store, pridce)를 갖도록 하는 방법을 작성해라. 특정 매장에 제품이 없는 경우, 해당 product_id와 store 조합에 대한 행은 결과 테이블에 포함하지 않는다.

Example

Input:
Products table:

product_idstore1store2store3
095100105
170null80

Output:

product_idstorepridct
0store195
0store2100
0store3105
1store170
1store380

Explanation:
제품 0은 모든 세 상점에서 각 95, 100, 105로 가능하다.
제품 1은 store1에서 70, store3에서 80으로 가능하다. 제품은 store2에서 불가능하다.

Solved

-- 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
profile
내 마음대로 코드 작성하는 세상

0개의 댓글