작성
·
18
·
수정됨
0
SELECT
title,
-- genres,
genre
FROM advanced.array_exercises AS ae
CROSS JOIN UNNEST(genres) AS genre
SELECT
title,
actor.actor,
actor.character
-- actors,
-- actors[SAFE_OFFSET(0)].actor AS actor,
-- actors[SAFE_OFFSET(1)].character AS character
FROM advanced.array_exercises
CROSS JOIN UNNEST(actors) AS actor
SELECT
title,
-- actors,
actor.actor,
actor.character,
genre
FROM advanced.array_exercises
CROSS JOIN UNNEST(actors) AS actor
CROSS JOIN UNNEST(genres) AS genre
SELECT
user_id,
event_date,
event_name,
user_pseudo_id,
event_param.key AS key,
-- event_param.value AS value,
event_param.value.string_value AS string_value,
event_param.value.int_value AS int_value
FROM advanced.app_logs
CROSS JOIN UNNEST(event_params) AS event_param
WHERE event_date = '2022-08-01'
1) orders 테이블에서 유저(user_id)별로 주문 금액(amount)의 합계를 PIVOT해주세요. 날짜(order_date)를 행(Row)으로, user_id를 열(Column)으로 만들어야 합니다
SELECT
order_date,
SUM(IF(user_id = 1, sum_of_amount, 0)) AS user_1,
SUM(IF(user_id = 2, sum_of_amount, 0)) AS user_2,
SUM(IF(user_id = 3, sum_of_amount, 0)) AS user_3
FROM
(SELECT
order_date,
user_id,
SUM(amount) as sum_of_amount
FROM advanced.orders
GROUP BY
order_date, user_id
)
GROUP BY order_date
ORDER BY order_date
SELECT
order_date,
SUM(IF(user_id = 1, amount, 0)) AS user_1,
SUM(IF(user_id = 2, amount, 0)) AS user_2,
SUM(IF(user_id = 3, amount, 0)) AS user_3
FROM
advanced.orders
GROUP BY order_date
ORDER BY order_date
2) orders 테이블에서 날짜(order_date)별로 유저들의 주문 금액(amount)의 합계를 PIVOT 해주세요. user_id를 행(Row)으로, order_date를 열(Column)으로 만들어야 합니다
SELECT
user_id,
SUM(IF(order_date = '2023-05-01', amount, 0)) AS `2023-05-01`,
SUM(IF(order_date = '2023-05-02', amount, 0)) AS `2023-05-02`,
SUM(IF(order_date = '2023-05-03', amount, 0)) AS `2023-05-03`,
SUM(IF(order_date = '2023-05-04', amount, 0)) AS `2023-05-04`,
SUM(IF(order_date = '2023-05-05', amount, 0)) AS `2023-05-05`
FROM
advanced.orders
GROUP BY user_id
ORDER BY user_id
3) orders 테이블에서 사용자(user_id)별, 날짜(order_date)별로 주문이 있다면 1, 없다면 0으로 PIVOT 해주세요. user_id를 행(Row)으로, order_date를 열(Column)로 만들고 주문을 많이 해도 1로 처리합니다
SELECT
user_id,
MAX(IF(order_date = '2023-05-01', 1, 0)) AS `2023-05-01`,
MAX(IF(order_date = '2023-05-02', 1, 0)) AS `2023-05-02`,
MAX(IF(order_date = '2023-05-03', 1, 0)) AS `2023-05-03`,
MAX(IF(order_date = '2023-05-04', 1, 0)) AS `2023-05-04`,
MAX(IF(order_date = '2023-05-05', 1, 0)) AS `2023-05-05`
FROM
advanced.orders
GROUP BY user_id
ORDER BY user_id
4) user_id = 32888이 카트 추가하기(click_cart)를 누를때 어떤 음식(food_id)을 담았나요?
SELECT
event_date,
event_timestamp,
event_name,
user_id,
user_pseudo_id,
MAX(IF(param.key = 'firebase_screen', param.value.string_value, null)) AS firebase_screen,
MAX(IF(param.key = 'food_id', param.value.int_value, null)) AS food_id
FROM advanced.app_logs
CROSS JOIN UNNEST(event_params) AS param
WHERE
user_id = 32888
and event_name = 'click_cart'
GROUP BY ALL
ORDER BY event_date
WITH base AS (
SELECT
event_date,
event_timestamp,
event_name,
user_id,
user_pseudo_id,
platform,
-- event_param
MAX(IF(event_param.key = "firebase_screen", event_param.value.string_value, NULL)) AS firebase_screen,
-- MAX(IF(event_param.key = "food_id", event_param.value.int_value, NULL)) AS food_id,
MAX(IF(event_param.key = "session_id", event_param.value.string_value, NULL)) AS session_id
FROM advanced.app_logs
CROSS JOIN UNNEST(event_params) AS event_param
WHERE
-- event_date = "2022-08-01" # 적은 데이터로 쿼리를 작성하기 위해 만들어둔 조건
event_date BETWEEN "2022-08-01" AND "2022-08-18"
GROUP BY ALL
), filter_event_and_concat_event_and_screen AS (
-- (1) event_name + screen (필요한 이벤트만 WHERE 조건에 걸어서 사용)
SELECT
* EXCEPT(event_name, firebase_screen, event_timestamp),
CONCAT(event_name, "-", firebase_screen) AS event_name_with_screen,
DATETIME(TIMESTAMP_MICROS(event_timestamp), 'Asia/Seoul') AS event_datetime
FROM base
WHERE
event_name IN ("screen_view", "click_payment")
),
funnel AS (
SELECT
event_date,
event_name_with_screen,
CASE
WHEN event_name_with_screen = "screen_view-welcome" THEN 1
WHEN event_name_with_screen = "screen_view-home" THEN 2
WHEN event_name_with_screen = "screen_view-food_category" THEN 3
WHEN event_name_with_screen = "screen_view-restaurant" THEN 4
WHEN event_name_with_screen = "screen_view-cart" THEN 5
WHEN event_name_with_screen = "click_payment-cart" THEN 6
ELSE NULL
END AS step_number,
COUNT(DISTINCT user_pseudo_id) AS cnt
FROM filter_event_and_concat_event_and_screen
GROUP BY ALL
HAVING step_number IS NOT NULL
)
SELECT
event_date,
MAX(IF(funnel.event_name_with_screen = 'screen_view-welcome', cnt, null)) as `screen_view-welcome`,
MAX(IF(funnel.event_name_with_screen = 'screen_view-home', cnt, null)) as `screen_view-home`,
MAX(IF(funnel.event_name_with_screen = 'screen_view-food_category', cnt, null)) as `screen_view-food_category`,
MAX(IF(funnel.event_name_with_screen = 'screen_view-restaurant', cnt, null)) as `screen_view-restaurant`,
MAX(IF(funnel.event_name_with_screen = 'screen_view-cart', cnt, null)) as `screen_view-cart`,
MAX(IF(funnel.event_name_with_screen = 'click_payment-cart', cnt, null)) as `click_payment-cart`
FROM funnel
GROUP BY ALL
ORDER BY event_date
답변 1
0
안녕하세요, 인프런 AI 인턴입니다. 이윤우님.
BigQuery와 관련된 다양한 연습 문제를 해결하시면서 ARRAY, STRUCT, PIVOT, 그리고 퍼널 쿼리 작성에 대해 깊이 있는 학습을 진행 중이시군요. 이를 통해 데이터 분석의 핵심 요소를 더욱 잘 이해할 수 있을 것입니다.
이와 관련하여 유사한 질문과 답변을 볼 수 있는 링크를 아래에 첨부합니다. 이 자료들이 도움이 될 수 있기를 바랍니다.
퍼널 SQL 작성 시 발생할 수 있는 실수와 해결 방법
ARRAY와 STRUCT를 활용하면 복잡한 데이터 구조를 보다 유연하게 다룰 수 있으며, PIVOT을 응용하면 데이터의 시각화를 통해 더 나은 인사이트를 얻을 수 있습니다. 퍼널 분석에서는 사용자의 행동을 단계별로 추적하여 중요한 결정을 내리는 데 큰 역할을 합니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.