ソースコード
WITH t1 AS (SELECT DISTINCT a.item_code a, b.item_code b
            FROM PURCHASE_HISTORY a
               , PURCHASE_HISTORY b
            WHERE a.item_code != b.item_code)
   , t2 AS (SELECT t1.a, t1.b, COUNT(DISTINCT pua.SESSION_ID) cnt
            FROM t1
                 JOIN PURCHASE_HISTORY pua ON pua.ITEM_CODE = t1.a
                 JOIN PURCHASE_HISTORY pub ON pub.ITEM_CODE = t1.b AND pua.SESSION_ID = pub.SESSION_ID
            GROUP BY t1.a, t1.b)
   , t3 AS (SELECT item_code, COUNT(*) cnt
            FROM PURCHASE_HISTORY pu
            GROUP BY pu.item_code)

SELECT a ITEM_A
     , b ITEM_B
     , ROUND(x * 100, 5) SUPPORT
     , ROUND(y * 100, 5) CONFIDENCE
     , ROUND(y / z, 5) LIFT
FROM (SELECT t1.a
           , t1.b
           , CAST(IFNULL(t2.cnt, 0) AS real) / (SELECT COUNT(DISTINCT session_id) FROM PURCHASE_HISTORY) x
           , CAST(IFNULL(t2.cnt, 0) AS real) / ac.cnt y
           , CAST(IFNULL(bc.cnt, 0) AS real) / (SELECT COUNT(DISTINCT session_id) FROM PURCHASE_HISTORY) z
      FROM t1
            JOIN t2 ON t1.a = t2.a AND t1.b = t2.b
           LEFT JOIN t3 ac ON t1.a = ac.ITEM_CODE
           LEFT JOIN t3 bc ON t1.b = bc.ITEM_CODE)
ORDER BY 5 DESC, 3 DESC, 4 DESC, 1 DESC;
提出情報
提出日時2024/06/19 15:08:28
コンテスト第11回 SQLコンテスト
問題アソシエーション分析
受験者pp1mqa6hkm
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量85 MB
メッセージ
テストケース(通過数/総数)
3/3
状態
メモリ使用量
データパターン1
AC
85 MB
データパターン2
AC
85 MB
データパターン3
AC
84 MB