ソースコード
WITH it as (
  SELECT distinct ITEM_CODE
  FROM PURCHASE_HISTORY
), I as (
    SELECT i1.ITEM_CODE ITEM_A, i2.ITEM_CODE ITEM_B
    FROM it i1, it i2
    WHERE i1.ITEM_CODE <> i2.ITEM_CODE
), U as (
    SELECT COUNT(*) users
    FROM (
    SELECT distinct SESSION_ID
    FROM PURCHASE_HISTORY
    )
), T as (
SELECT
   ITEM_A
  ,ITEM_B
  ,P1.SESSION_ID
FROM I
LEFT JOIN PURCHASE_HISTORY P1 ON ITEM_A = P1.ITEM_CODE
LEFT JOIN PURCHASE_HISTORY P2 ON ITEM_B = P2.ITEM_CODE
WHERE P1.SESSION_ID = P2.SESSION_ID
)

SELECT
  ITEM_A
 ,ITEM_B
 ,round(COUNT(*) * 100.0 / (SELECT users FROM U), 5) as SUPPORT
 ,round((COUNT(*) * 100.0 / (SELECT COUNT(*) FROM PURCHASE_HISTORY WHERE ITEM_CODE = ITEM_A)), 5) as CONFIDENCE
 ,round(
    (COUNT(*) * 1.0 / (SELECT COUNT(*) FROM PURCHASE_HISTORY WHERE ITEM_CODE = ITEM_A)) /
    ((SELECT COUNT(*) FROM PURCHASE_HISTORY WHERE ITEM_CODE = ITEM_B) * 1.0 / (SELECT users FROM U)), 5) as LIFT
FROM T
GROUP BY ITEM_A, ITEM_B
ORDER BY 5 desc, 3 desc, 4 desc, 1 desc
提出情報
提出日時2024/02/18 07:17:34
コンテスト第11回 SQLコンテスト
問題アソシエーション分析
受験者mine
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量84 MB
メッセージ
テストケース(通過数/総数)
3/3
状態
メモリ使用量
データパターン1
AC
84 MB
データパターン2
AC
84 MB
データパターン3
AC
84 MB