來自傳統SQL的關於LAST
的事情是,輸出對於您給出的示例是未定義的;它沒有指定在這種情況下如何確定「最後」,因爲輸入表掃描不能保證有任何特定的順序。由於寫的,你可以表達從BigQuery中的標準SQL與ANY_VALUE
此相同的查詢,例如:
WITH SampleInput AS (
SELECT 1 AS id, 'apple' AS name UNION ALL
SELECT 1, 'banana' UNION ALL
SELECT 2, 'carrot' UNION ALL
SELECT 3, 'lemon' UNION ALL
SELECT 3, 'orange'
)
SELECT
id,
ANY_VALUE(name) AS last_record_of_name
FROM SampleInput
GROUP BY id;
+----+---------------------+
| id | last_record_of_name |
+----+---------------------+
| 1 | apple |
| 2 | carrot |
| 3 | lemon |
+----+---------------------+
我不認爲這是你以後,雖然,如果我們的目標是讓基於一些標準,如name
值的排列順序的「最後」的值,那麼你可以使用ARRAY_AGG
與ORDER BY
和LIMIT 1
,如:
WITH SampleInput AS (
SELECT 1 AS id, 'apple' AS name UNION ALL
SELECT 1, 'banana' UNION ALL
SELECT 2, 'carrot' UNION ALL
SELECT 3, 'lemon' UNION ALL
SELECT 3, 'orange'
)
SELECT
id,
ARRAY_AGG(name ORDER BY name DESC LIMIT 1)[OFFSET(0)] AS last_record_of_name
FROM SampleInput
GROUP BY id;
+----+---------------------+
| id | last_record_of_name |
+----+---------------------+
| 1 | banana |
| 2 | carrot |
| 3 | orange |
+----+---------------------+
查詢的行爲定義明確,並根據您的樣本輸入和輸出提供期望的結果。