쿼리의, 쿼리에 의한, 쿼리를 위한 yammer case study...

1) 문제 상황 파악

2) Yammer 주니어들의 쿼리 이해

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/907032f3-fb71-4a6f-91b0-026aefaccb2b/_2png.png

                                                           각 table 전체 구성

BOUNDS 테이블

SELECT user_id,
       event_type,
       event_name,
       occurred_at,
       occurred_at - LAG(occurred_at,1) OVER (PARTITION BY user_id ORDER BY occurred_at) AS last_event,
       LEAD(occurred_at,1) OVER (PARTITION BY user_id ORDER BY occurred_at) - occurred_at AS next_event,
       ROW_NUMBER() OVER () AS id -- 테이블에 넘버링 후 컬럼네임은 id라고 하자.
FROM tutorial.yammer_events e
WHERE e.event_type = 'engagement'
ORDER BY user_id,occurred_at

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d49b28f0-8e32-4630-909e-0a94410073b9/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/118a5647-fd92-4d7d-b069-3ad125970c09/Untitled.png

FINAL 테이블

SELECT *, -- 실제쿼리에서는 bounds.*
       **CASE WHEN last_event >= INTERVAL '10 MINUTE' THEN id
            WHEN last_event IS NULL THEN id
            ELSE LAG(id,1) OVER (PARTITION BY user_id ORDER BY occurred_at) END AS session**
FROM **bounds** -- (실제 쿼리에서는 STEP 1의 SELECT문)
WHERE last_event >= INTERVAL '10 MINUTE'
   OR next_event >= INTERVAL '10 MINUTE'
   OR last_event IS NULL
   OR next_event IS NULL

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/28a7f181-6814-4e2b-baae-ce9939f79e79/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/59c5bb02-adfc-414a-8e0e-8d571dc62cf7/Untitled.png