ソースコード
WITH
    OrderedLogs AS (
        SELECT
            SESSION_ID
          , PROCESS_ID
          , USER_ID
          , EX_TIMESTAMP
          , LAG (PROCESS_ID) OVER (
                PARTITION BY
                    SESSION_ID
                ORDER BY
                    EX_TIMESTAMP
            ) AS PreviousProcess
        FROM
            PROCESS_LOG
    )
  , FilteredLogs AS (
        SELECT
            SESSION_ID
          , PROCESS_ID
          , USER_ID
          , EX_TIMESTAMP
        FROM
            OrderedLogs
        WHERE
            (
                PROCESS_ID = 'STEP1'
                AND PreviousProcess IS NULL
            )
            OR (
                PROCESS_ID = 'STEP2'
                AND PreviousProcess = 'STEP1'
            )
            OR (
                PROCESS_ID = 'STEP3'
                AND PreviousProcess = 'STEP2'
            )
            OR (
                PROCESS_ID = 'STEP4'
                AND PreviousProcess = 'STEP3'
            )
            OR (
                PROCESS_ID = 'STEP5'
                AND PreviousProcess = 'STEP4'
            )
    )
  , session1 AS (
        SELECT
            SESSION_ID
          , USER_ID
          , MIN(EX_TIMESTAMP) AS min_EX_TIMESTAMP
        FROM
            FilteredLogs
        WHERE
            PROCESS_ID = 'STEP1'
        GROUP BY
            SESSION_ID
          , USER_ID
    )
  , session2 AS (
        SELECT
            p.SESSION_ID
          , p.USER_ID
          , MIN(p.EX_TIMESTAMP) AS min_EX_TIMESTAMP
        FROM
            FilteredLogs p
            INNER JOIN session1 s1 ON p.SESSION_ID = s1.SESSION_ID
            AND p.USER_ID = s1.USER_ID
        WHERE
            p.PROCESS_ID = 'STEP2'
            AND p.EX_TIMESTAMP > s1.min_EX_TIMESTAMP
        GROUP BY
            p.SESSION_ID
          , p.USER_ID
    )
  , session3 AS (
        SELECT
            p.SESSION_ID
          , p.USER_ID
          , MIN(p.EX_TIMESTAMP) AS min_EX_TIMESTAMP
        FROM
            FilteredLogs p
            INNER JOIN session2 s2 ON p.SESSION_ID = s2.SESSION_ID
            AND p.USER_ID = s2.USER_ID
        WHERE
            p.PROCESS_ID = 'STEP3'
            AND p.EX_TIMESTAMP > s2.min_EX_TIMESTAMP
        GROUP BY
            p.SESSION_ID
          , p.USER_ID
    )
  , session4 AS (
        SELECT
            p.SESSION_ID
          , p.USER_ID
          , MIN(p.EX_TIMESTAMP) AS min_EX_TIMESTAMP
        FROM
            FilteredLogs p
            INNER JOIN session3 s3 ON p.SESSION_ID = s3.SESSION_ID
            AND p.USER_ID = s3.USER_ID
        WHERE
            p.PROCESS_ID = 'STEP4'
            AND p.EX_TIMESTAMP > s3.min_EX_TIMESTAMP
        GROUP BY
            p.SESSION_ID
          , p.USER_ID
    )
  , session5 AS (
        SELECT
            p.SESSION_ID
          , p.USER_ID
          , MIN(p.EX_TIMESTAMP) AS min_EX_TIMESTAMP
        FROM
            FilteredLogs p
            INNER JOIN session4 s4 ON p.SESSION_ID = s4.SESSION_ID
            AND p.USER_ID = s4.USER_ID
        WHERE
            p.PROCESS_ID = 'STEP5'
            AND p.EX_TIMESTAMP > s4.min_EX_TIMESTAMP
        GROUP BY
            p.SESSION_ID
          , p.USER_ID
    )
  , final_counts AS (
        SELECT
            'STEP1' AS PROCESS
          , COUNT(DISTINCT SESSION_ID) AS CNT
        FROM
            session1
        UNION ALL
        SELECT
            'STEP2'
          , COUNT(DISTINCT SESSION_ID)
        FROM
            session2
        UNION ALL
        SELECT
            'STEP3'
          , COUNT(DISTINCT SESSION_ID)
        FROM
            session3
        UNION ALL
        SELECT
            'STEP4'
          , COUNT(DISTINCT SESSION_ID)
        FROM
            session4
        UNION ALL
        SELECT
            'STEP5'
          , COUNT(DISTINCT SESSION_ID)
        FROM
            session5
    )
SELECT
    *
FROM
    final_counts
ORDER BY
    PROCESS;
提出情報
提出日時2023/12/17 06:37:32
コンテスト第10回 SQLコンテスト
問題顧客行動分析
受験者hiraku
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量87 MB
メッセージ
テストケース(通過数/総数)
4/4
状態
メモリ使用量
データパターン1
AC
85 MB
データパターン2
AC
86 MB
データパターン3
AC
86 MB
データパターン4
AC
87 MB