ソースコード
WITH ORDER_SUM AS (
SELECT DTL.ITEM_CODE,
MAX(OD.ORDER_DATE) AS ORDER_DATE,
SUM(DTL.ORDER_QTY) AS ORDER_QTY
FROM ORDERS OD
INNER JOIN ORDERS_DTL DTL
ON OD.ORDER_NO = DTL.ORDER_NO
WHERE OD.ORDER_DATE >= '2023-04-01'
AND OD.ORDER_DATE <= '2023-06-30'
GROUP BY DTL.ITEM_CODE
),RANKING AS (
SELECT IT.ITEM_CODE,
IT.ITEM_NAME,
OD.ORDER_DATE,
COALESCE(OD.ORDER_QTY,0) AS ORDER_QTY,
RANK()OVER(ORDER BY COALESCE(OD.ORDER_QTY,0) DESC,OD.ORDER_DATE DESC,IT.ITEM_CODE DESC) AS ITEM_POPULAR_RANK 
FROM ITEM IT
LEFT OUTER JOIN ORDER_SUM OD
ON IT.ITEM_CODE = OD.ITEM_CODE
)
UPDATE ITEM
SET ITEM_POPULAR_RANK = RANKING.ITEM_POPULAR_RANK
FROM (
SELECT ITEM_CODE,
CASE WHEN ORDER_QTY = 0 THEN 0
ELSE ITEM_POPULAR_RANK END AS ITEM_POPULAR_RANK
FROM RANKING
) AS RANKING
WHERE ITEM.ITEM_CODE = RANKING.ITEM_CODE
提出情報
提出日時2023/08/20 22:08:16
コンテスト第8回 SQLコンテスト
問題人気順位
受験者takahiro
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量81 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
81 MB
データパターン2
AC
79 MB