작성
·
29
·
수정됨
0
SELECT
*,
LEAD(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) AS next_visit_month,
LEAD(visit_month, 2) OVER(PARTITION BY user_id ORDER BY visit_month) AS second_next_visit_month
FROM `bqmaster.advanced.analytics_function_01`
ORDER BY user_id
SELECT
*,
LEAD(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) AS next_visit_month,
LEAD(visit_month, 2) OVER(PARTITION BY user_id ORDER BY visit_month) AS second_next_visit_month,
LAG(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) AS previous_visit_month
FROM `bqmaster.advanced.analytics_function_01`
ORDER BY user_id
SELECT
*,
LEAD(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) - visit_month AS next_visit_month_diff
FROM `bqmaster.advanced.analytics_function_01`
ORDER BY user_id
-- 서브 쿼리 활용
SELECT
user_id,
visit_month,
next_visit_month - visit_month AS next_visit_month_diff
FROM (
SELECT
*,
LEAD(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) AS next_visit_month
FROM `bqmaster.advanced.analytics_function_01`
)
ORDER BY user_id
SELECT
*,
FIRST_VALUE(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) AS first_visit_month,
LAST_VALUE(visit_month) OVER(PARTITION BY user_id ORDER BY visit_month) AS last_visit_month
FROM `bqmaster.advanced.analytics_function_01`
ORDER BY user_id
SELECT
*,
SUM(amount) OVER() AS amount_total,
SUM(amount) OVER(ORDER BY order_id) AS cumulative_sum,
SUM(amount) OVER(PARTITION BY user_id ORDER BY order_id) AS cumulative_sum_by_user,
AVG(amount) OVER(ORDER BY order_id ROWS BETWEEN 5 PRECEDING AND 1 PRECEDING) AS last_5_orders_avg_amount
FROM `bqmaster.advanced.orders`
ORDER BY order_id
SELECT
*,
COUNT(user) OVER(PARTITION BY user) AS total_query_cnt
FROM `bqmaster.advanced.query_logs`
ORDER BY query_date
SELECT
*,
RANK() OVER(PARTITION BY week_number, team ORDER BY query_cnt DESC) AS team_rank
FROM (
SELECT
EXTRACT(WEEK FROM query_date) AS week_number,
team,
user,
COUNT(user) query_cnt
FROM `bqmaster.advanced.query_logs`
GROUP BY ALL
)
QUALIFY
team_rank = 1
ORDER BY week_number, team
SELECT
*,
LAG(query_count) OVER(PARTITION BY user ORDER BY week_number) AS prev_week_query_count
FROM (
SELECT
user,
team,
EXTRACT(WEEK FROM query_date) AS week_number,
COUNT(user) query_count
FROM `bqmaster.advanced.query_logs`
GROUP BY ALL
)
ORDER BY user, team
SELECT
*,
SUM(query_count) OVER(PARTITION BY user ORDER BY query_date) AS cumulative_query_count
FROM (
SELECT
*,
COUNT(user) AS query_count
FROM `bqmaster.advanced.query_logs`
GROUP BY ALL
)
ORDER BY user, query_date
SELECT
date,
LAST_VALUE(number_of_orders IGNORE NULLS) OVER(ORDER BY date) AS number
FROM raw_data
ORDER BY date
SELECT
*,
AVG(number) OVER(ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg
FROM (
SELECT
date,
LAST_VALUE(number_of_orders IGNORE NULLS) OVER(ORDER BY date) AS number
FROM raw_data
)
ORDER BY date
WITH base AS (
SELECT
event_date,
DATETIME(TIMESTAMP_MICROS(event_timestamp), 'Asia/Seoul') AS event_datetime,
event_name,
user_id,
user_pseudo_id
FROM `bqmaster.advanced.app_logs`
), add_before_datetime AS(
SELECT
*,
LAG(event_datetime) OVER(PARTITION BY user_pseudo_id ORDER BY event_datetime) AS before_event_datetime
FROM base
), add_second_diff AS (
SELECT
*,
DATE_DIFF(event_datetime, before_event_datetime, SECOND) AS second_diff
FROM add_before_datetime
), add_session_start AS (
SELECT
*,
CASE
WHEN second_diff IS NULL THEN 1
WHEN second_diff >= 20 THEN 1
ELSE NULL
END AS session_start
FROM add_second_diff
)
SELECT
*,
SUM(session_start) OVER(PARTITION BY user_pseudo_id ORDER BY event_datetime) AS session_id
FROM add_session_start
ORDER BY event_datetime
답변 1
0
안녕하세요, 인프런 AI 인턴입니다. 차붐기붐님.
윈도우 함수인 LEAD를 활용하여 사용자들의 다음 접속 월과 다다음 접속 월을 구하는 쿼리를 작성하셨네요. LEAD 함수는 특정 컬럼의 다음 행 값을 가져오는 데 사용되며, 이 경우 ‘visit_month’ 컬럼을 기준으로 작동하고 있습니다.
질문에 대한 유사한 사례를 제공할 수 있는 링크를 아래에 첨부하겠습니다:
이 게시물에서는 윈도우 함수를 활용한 분석 예제가 있어 차붐기붐님의 쿼리 작성에 더 많은 아이디어를 제공할 수 있을 것입니다. 특히 LEAD와 LAG 함수의 사용법 및 응용 예를 확인할 수 있습니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.