https://school.programmers.co.kr/learn/courses/30/lessons/133027
정답
SELECT FLAVOR
FROM(
SELECT ROWNUM RN,T.*
FROM (
SELECT A.FLAVOR, SUM(B.TOTAL_ORDER)+A.TOTAL_ORDER TOTAL
FROM FIRST_HALF A
INNER JOIN JULY B
ON A.FLAVOR= B.FLAVOR
GROUP BY A.FLAVOR,A.TOTAL_ORDER
ORDER BY TOTAL DESC
) T
)R
WHERE R.RN <=3;
풀이
Step 1) from 절 group by
SELECT A.FLAVOR,A.TOTAL_ORDER as A테이블_TOTAL_ORDER,B.TOTAL_ORDER as B테이블_TOTAL_ORDER
FROM FIRST_HALF A
INNER JOIN JULY B
ON A.FLAVOR= B.FLAVOR
- A테이블 TOTAL_ORDER 컬럼은 값이 동일하기 때문에 GROUP BY 안함
SELECT A.FLAVOR, SUM(B.TOTAL_ORDER)+A.TOTAL_ORDER TOTAL
FROM FIRST_HALF A
INNER JOIN JULY B
ON A.FLAVOR= B.FLAVOR
GROUP BY A.FLAVOR,A.TOTAL_ORDER
ORDER BY TOTAL DESC
Step 2) ROWNUM
SELECT ROWNUM RN,T.*
FROM (
SELECT A.FLAVOR, SUM(B.TOTAL_ORDER)+A.TOTAL_ORDER TOTAL
FROM FIRST_HALF A
INNER JOIN JULY B
ON A.FLAVOR= B.FLAVOR
GROUP BY A.FLAVOR,A.TOTAL_ORDER
ORDER BY TOTAL DESC
) T
Step 3) ROWNUM 조건 <=3
SELECT FLAVOR
FROM(
SELECT ROWNUM RN,T.*
FROM (
SELECT A.FLAVOR, SUM(B.TOTAL_ORDER)+A.TOTAL_ORDER TOTAL
FROM FIRST_HALF A
INNER JOIN JULY B
ON A.FLAVOR= B.FLAVOR
GROUP BY A.FLAVOR,A.TOTAL_ORDER
ORDER BY TOTAL DESC
) T
)R
WHERE R.RN <=3;
'DB > 프로그래머스' 카테고리의 다른 글
[Oracle] 오프라인/온라인 판매 데이터 통합하기 (0) | 2024.10.06 |
---|---|
[Oracle] 취소되지 않은 진료 예약 조회하기 (0) | 2024.10.02 |
[Oracle] 저자 별 카테고리 별 매출액 집계하기 (0) | 2024.09.28 |
[Oracle] 오랜 기간 보호한 동물(2) (0) | 2024.09.23 |
[Oracle] 즐겨찾기가 가장 많은 식당 정보 출력하기 (1) | 2024.09.23 |