/* aggregated sales by year-month */
SELECT
T.CALENDAR_YEAR AS Year,
T.CALENDAR_MONTH_DESC AS Month,
SUM(S.AMOUNT_SOLD) AS AmountOfMoney
FROM
SALES S
JOIN
TIMES T
ON T.TIME_ID = S.TIME_ID
WHERE S.PROD_ID = (SELECT PROD_ID
FROM (
SELECT
S.PROD_ID,
SUM(S.AMOUNT_SOLD) AMOUNT
FROM
SALES S
GROUP BY S.PROD_ID
ORDER BY SUM(S.AMOUNT_SOLD) DESC
) BS
WHERE ROWNUM = 1
)
GROUP BY
T.CALENDAR_YEAR,
T.CALENDAR_MONTH_DESC
ORDER BY
T.CALENDAR_YEAR,
T.CALENDAR_MONTH_DESC;
我不使用 Oracle,但使用 STD 语法应该可以:
db<>在这里摆弄