UNION
-
프로그래머스 - 오프라인/온라인 판매 데이터 통합하기SQL 문제 2024. 1. 9. 14:50
오프라인/ 온라인 판매 데이터 통합하기 ONLINE_SALE 테이블과 OFFLINE_SALE 테이블에서 2022년 3월의 오프라인/온라인 상품 판매 데이터의 판매 날짜, 상품ID, 유저ID, 판매량을 출력하는 SQL문을 작성해주세요. OFFLINE_SALE 테이블의 판매 데이터의 USER_ID 값은 NULL 로 표시해주세요. 결과는 판매일을 기준으로 오름차순 정렬해주시고 판매일이 같다면 상품 ID를 기준으로 오름차순, 상품ID까지 같다면 유저 ID를 기준으로 오름차순 정렬해주세요. 문제를 읽고서 처음에 해낸 생각은 JOIN을 써야겠다라고 생각해다. 코드를 작성하다보니 뭔가 이상했다. 두개의 테이블을 JOIN을 하면 코드가 안나오기 때문이다. 그래서 문제를 다시 읽고 해석해보니 각 테이블의 SALE_DAT..
-
12/5스파르타/TIL(Today I Learned) 2023. 12. 5. 16:30
오늘도 어김없이 프로그래머스 문제를 풀다가 막혀 !도움을 하던중 정말 깔끔하게 짠 코드를 보고 이해하며 감탄한 코드가 있어 가져와봤다. 문제 : 데이터 분석 팀에서는 우유(Milk)와 요거트(Yogurt)를 동시에 구입한 장바구니가 있는지 알아보려 합니다. 우유와 요거트를 동시에 구입한 장바구니의 아이디를 조회하는 SQL 문을 작성해주세요. 이때 결과는 장바구니의 아이디 순으로 나와야 합니다. cart_products의 테이블이다. 보면 name에 두가지 조건 1. name = 'Milk' 2. name = 'Yogurt' 를 동시에 만족해야 한다 하지만 그냥 코드를 한번에 작성해서 하려면 두 조건을 동시에 만족하기는 어렵다. 그래서 서브쿼리로 조건 한개를 처리한 후 나머지 조건을 처리하는 방법을 소개하..