-
[프로그래머스] 5월 식품들의 총매출 조회하기 MYSQLCoding Test/programmers 2023. 1. 6. 10:44728x90
문제 설명
FOOD_PRODUCT와 FOOD_ORDER 테이블에서 생산일자가 2022년 5월인 식품들의 식품 ID, 식품 이름, 총매출을 조회하는 SQL문을 작성해주세요. 이때 결과는 총매출을 기준으로 내림차순 정렬해주시고 총매출이 같다면 식품 ID를 기준으로 오름차순 정렬해주세요.
풀이
SELECT P.PRODUCT_ID, P.PRODUCT_NAME, SUM(P.PRICE * O.AMOUNT) AS TOTAL_SALES FROM FOOD_PRODUCT P LEFT JOIN FOOD_ORDER O ON P.PRODUCT_ID = O.PRODUCT_ID WHERE O.PRODUCE_DATE LIKE '2022-05%' GROUP BY P.PRODUCT_ID ORDER BY TOTAL_SALES DESC, P.PRODUCT_ID ASC;
다른 방법
SELECT P.PRODUCT_ID, P.PRODUCT_NAME, SUM(P.PRICE * O.AMOUNT) AS TOTAL_SALES FROM FOOD_PRODUCT P JOIN (SELECT * FROM FOOD_ORDER) O ON P.PRODUCT_ID = O.PRODUCT_ID WHERE O.PRODUCE_DATE LIKE '2022-05%' GROUP BY P.PRODUCT_ID ORDER BY TOTAL_SALES DESC, P.PRODUCT_ID ASC;
728x90'Coding Test > programmers' 카테고리의 다른 글
[프로그래머스] 보호소에서 중성화한 동물 MYSQL (0) 2023.01.06 [프로그래머스] 그룹별 조건에 맞는 식당 목록 출력하기 MYSQL (0) 2023.01.06 [프로그래머스] 주문량이 많은 아이스크림들 조회하기 MYSQL (0) 2023.01.06 [프로그래머스] 상품 별 오프라인 매출 구하기 MYSQL (0) 2023.01.06 [프로그래머스] 오랜 기간 보호한 동물(1) mysql (0) 2023.01.05