考慮下面爲如何分析事件表進行有意義的信息的例子:
這個例子使用的BigQuery標準SQL,所以你需要取消Use Legacy SQL
下顯示選項複選框
WITH order_events AS (
SELECT 1 AS orderID, '2015-01-01' AS ts, 'created' AS event UNION ALL
SELECT 1 AS orderID, '2015-01-01' AS ts, 'shopping' AS event UNION ALL
SELECT 1 AS orderID, '2015-01-02' AS ts, 'completed' AS event UNION ALL
SELECT 1 AS orderID, '2015-01-03' AS ts, 'confirmed' AS event UNION ALL
SELECT 1 AS orderID, '2015-01-04' AS ts, 'shipped' AS event UNION ALL
SELECT 2 AS orderID, '2015-01-01' AS ts, 'created' AS event UNION ALL
SELECT 2 AS orderID, '2015-01-01' AS ts, 'shopping' AS event UNION ALL
SELECT 2 AS orderID, '2015-01-02' AS ts, 'completed' AS event UNION ALL
SELECT 2 AS orderID, '2015-01-03' AS ts, 'declined' AS event UNION ALL
SELECT 3 AS orderID, '2015-01-01' AS ts, 'created' AS event UNION ALL
SELECT 3 AS orderID, '2015-01-01' AS ts, 'shopping' AS event UNION ALL
SELECT 3 AS orderID, '2015-01-02' AS ts, 'completed' AS event UNION ALL
SELECT 3 AS orderID, '2015-01-03' AS ts, 'confirmed' AS event UNION ALL
SELECT 3 AS orderID, '2015-01-04' AS ts, 'shipped' AS event UNION ALL
SELECT 4 AS orderID, '2015-01-01' AS ts, 'created' AS event UNION ALL
SELECT 4 AS orderID, '2015-01-01' AS ts, 'shopping' AS event UNION ALL
SELECT 4 AS orderID, '2015-01-02' AS ts, 'completed' AS event UNION ALL
SELECT 4 AS orderID, '2015-01-03' AS ts, 'confirmed' AS event UNION ALL
SELECT 4 AS orderID, '2015-01-05' AS ts, 'canceled' AS event UNION ALL
SELECT 5 AS orderID, '2015-01-01' AS ts, 'created' AS event UNION ALL
SELECT 5 AS orderID, '2015-01-01' AS ts, 'shopping' AS event
),
order_history AS (
SELECT
orderID,
(SELECT STRING_AGG(events, ' > ') FROM t.events) AS history
FROM (
SELECT
orderID,
ARRAY(SELECT event FROM t.events ORDER BY ts ASC) events
FROM (
SELECT
orderID,
ARRAY_AGG(STRUCT(event, ts)) events
FROM order_events
GROUP BY orderID
) t
) t
)
SELECT *
FROM order_history
#WHERE REGEXP_EXTRACT(history, r'((?:created).*(?:canceled))') IS NOT NULL
結果上面形象化歷史訂單作爲
現在,如果您將取消與WHERE
子句的最後一行的註釋 - 您將只獲得與給定模式匹配的訂單 - 在此情況下:創建但取消的訂單。
orderID history
4 created > shopping > completed > confirmed > canceled
設置適當的正則表達式爲您提供了靈活性,對任何過濾器需要分析
希望這給你出個主意,你可以擴展您的具體需求!
通常,BigQuery附帶包含ga_sessions表的數據集(如果您有高級帳戶)。你的賬戶裏有這個數據集嗎? –