작성
·
86
·
수정됨
0
SELECT [1, 2, 3, 4, 5] AS some_numbers
;
SELECT ARRAY<INT64>[1, 2, 3, 4, 5] AS some_numbers
;
SELECT GENERATE_ARRAY(1, 5, 1) AS some_numbers
;
SELECT [SAFE_OFFSET()]
;
SELECT (1, 2, 3) AS struct_test
;
SELECT STRUCT<hi INT64, hello INT64>(1, 2) AS struct_test
;
SELECT a.title, b AS genre
FROM workspace.array_exercises AS a
JOIN UNNEST(genres) AS b
;
SELECT a.title, b.actor, b.character
FROM workspace.array_exercises AS a
JOIN UNNEST(actors) AS b
;
SELECT a.title, b.actor, b.character, c AS genre
FROM workspace.array_exercises AS a
JOIN UNNEST(actors) AS b
JOIN UNNEST(genres) AS c
;
SELECT a.user_id, a.event_date, a.event_name, a.user_pseudo_id, b.key, b.value.string_value, b.value.int_value
FROM workspace.app_logs AS a
JOIN UNNEST(event_params) AS b
;
SELECT key, string_value, count(distinct user_pseudo_id)
FROM
(
SELECT a.user_id, a.event_date, a.event_name, a.user_pseudo_id, b.key, b.value.string_value, b.value.int_value
FROM workspace.app_logs AS a
JOIN UNNEST(event_params) AS b
)
WHERE event_name = 'screen_view'
GROUP BY ALL
;
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 workspace.orders
GROUP BY ALL
ORDER BY order_date
;
SELECT
user_id,
sum(if(order_date = '2023-05-01', amount, 0)) `2023-05-01`,
sum(if(order_date = '2023-05-02', amount, 0)) `2023-05-02`,
sum(if(order_date = '2023-05-03', amount, 0)) `2023-05-03`,
sum(if(order_date = '2023-05-04', amount, 0)) `2023-05-04`,
sum(if(order_date = '2023-05-05', amount, 0)) `2023-05-05`
FROM workspace.orders
GROUP BY ALL
ORDER BY user_id
;
SELECT
user_id,
if(sum(if(order_date = '2023-05-01', amount, 0)) > 0, 1, 0) `2023-05-01`,
if(sum(if(order_date = '2023-05-02', amount, 0)) > 0, 1, 0) `2023-05-02`,
if(sum(if(order_date = '2023-05-03', amount, 0)) > 0, 1, 0) `2023-05-03`,
if(sum(if(order_date = '2023-05-04', amount, 0)) > 0, 1, 0) `2023-05-04`,
if(sum(if(order_date = '2023-05-05', amount, 0)) > 0, 1, 0) `2023-05-05`
FROM workspace.orders
GROUP BY ALL
ORDER BY user_id
;
WITH events AS (
SELECT
event_date,
event_timestamp,
event_name,
user_id,
user_pseudo_id,
platform,
MAX(IF(b.key = "firebase_screen", b.value.string_value, NULL)) AS firebase_screen,
MAX(IF(b.key = "session_id", b.value.string_value, NULL)) AS session_id
FROM workspace.app_logs AS a
JOIN UNNEST(event_params) AS b
WHERE event_date >= '2022-08-01'
AND event_date < '2022-08-19'
GROUP BY ALL
),
filter_event_and_concat_event_and_screen AS (
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 events
WHERE event_name IN ('screen_view', 'click_payment')
)
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 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
;
답변 1
0
안녕하세요, 인프런 AI 인턴입니다. MS님.
BigQuery에서 ARRAY와 STRUCT, 그리고 PIVOT를 활용하여 SQL 쿼리를 작성하는 연습 문제를 수행 중이시군요. 다양한 자료형을 다루면서 BigQuery의 고급 기능들을 이해하고 이를 통해 원하는 데이터를 효과적으로 조회하는 것은 매우 중요한 과정입니다.
이와 관련하여 퍼널 분석을 이해하는 데 도움이 될 수 있는 유사한 질문들을 정리했습니다. 해당 쿼리들에서 ARRAY와 STRUCT의 사용 방법, 데이터를 PIVOT 하는 방법 등의 예시를 참고해 보실 수 있습니다.
ARRAY, STRUCT 활용 예시
PIVOT 적용 및 오류 해결 사례
비슷한 방식의 데이터 분석
위 질문과 답변을 통해 ARRAY, STRUCT, PIVOT 등을 더욱 깊이 이해할 수 있을 것입니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.