與BigQuery標準SQL - 試試下面
(確保取消Use Legacy SQL
複選框下顯示選項)
WITH YourTable AS (
SELECT 1 AS ts, 'y' AS event_type, 'true value' AS some_value UNION ALL
SELECT 2 AS ts, 'x' AS event_type, 'not relevant' AS some_value UNION ALL
SELECT 3 AS ts, 'y' AS event_type, 'false value' AS some_value UNION ALL
SELECT 4 AS ts, 'x' AS event_type, 'not relevant2' AS some_value UNION ALL
SELECT 5 AS ts, 'y' AS event_type, 'true value' AS some_value UNION ALL
SELECT 6 AS ts, 'y' AS event_type, 'false value' AS some_value UNION ALL
SELECT 7 AS ts, 'x' AS event_type, 'not relevant3' AS some_value UNION ALL
SELECT 8 AS ts, 'x' AS event_type, 'not relevant4' AS some_value
)
SELECT
event_type,
some_value,
(SELECT some_value = 'true value' FROM YourTable
WHERE event_type = 'y' AND ts < a.ts
ORDER BY ts DESC LIMIT 1
) AS previous_true
FROM YourTable AS a
WHERE event_type = 'x'
ORDER BY ts
結果是:
event_type some_value previous_true
x not relevant true
x not relevant2 false
x not relevant3 false
x not relevant4 false
對於傳統的BigQuery SQL - 嘗試
SELECT
event_type, some_value,
previous_true = 'true value' AS previous_true
FROM (
SELECT
ts, event_type, some_value,
FIRST_VALUE(some_value) OVER(PARTITION BY grp ORDER BY ts) AS previous_true
FROM (
SELECT
ts, event_type, some_value,
SUM(step) OVER(ORDER BY ts) AS grp
FROM (
SELECT
ts, event_type, some_value,
IF(event_type = 'x' , 0, 1) AS step
FROM
(SELECT 1 AS ts, 'y' AS event_type, 'true value' AS some_value),
(SELECT 2 AS ts, 'x' AS event_type, 'not relevant' AS some_value),
(SELECT 3 AS ts, 'y' AS event_type, 'false value' AS some_value),
(SELECT 4 AS ts, 'x' AS event_type, 'not relevant2' AS some_value),
(SELECT 5 AS ts, 'y' AS event_type, 'true value' AS some_value),
(SELECT 6 AS ts, 'y' AS event_type, 'false value' AS some_value),
(SELECT 7 AS ts, 'x' AS event_type, 'not relevant3' AS some_value),
(SELECT 8 AS ts, 'x' AS event_type, 'not relevant4' AS some_value)
)
)
)
WHERE event_type = 'x'
ORDER BY ts
所以傳統的工作就好了。但是,當我嘗試使用非傳統版本時遇到了問題。當我爲'YourTable'替換'mydataset.table'時,我得到了'Query is not supported'錯誤。這是我能找到的錯誤信息的全部範圍。 –
如果你需要這個來使用標準的sql工作,但仍然有自己的問題需要解決 - 請提交更多的細節問題(確切的查詢等) - 評論的格式不允許在這種情況下提供有效的幫助 - 我會非常樂意在那裏幫助 –