[LeetCode] 1795. Rearrange Products Table

Chobby·4일 전
1

LeetCode

목록 보기
605/650

😎풀이

  1. store1NULL이 아니라면, store1 점수 출력
  2. store2NULL이 아니라면, store2 점수 출력
  3. store3NULL이 아니라면, store3 점수 출력
  4. 해당 컬럼을 UNION으로 엮어 같이 조회회
SELECT 
    p1.product_id AS product_id, 
    'store1' AS store,
    p1.store1 AS price
FROM Products AS p1 
WHERE p1.store1 IS NOT NULL
UNION
SELECT 
    p2.product_id AS product_id, 
    'store2' AS store,
    p2.store2 AS p2
FROM Products AS p2 
WHERE p2.store2 IS NOT NULL
UNION
SELECT 
    p3.product_id AS product_id, 
    'store3' AS store,
    p3.store3 AS price
FROM Products AS p3 
WHERE p3.store3 IS NOT NULL
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글