2017-10-19 94 views
0

我有標識和名稱的表BIGQUERY標準的SQL相當於去年()從傳統的SQL

id | name 
---------- 
1 | apple 
1 | banana 
2 | carrot 
3 | lemon 
3 | orange 

在傳統的SQL,一個能寫出像

SELECT 
    id, 
    LAST(name) AS last_record_of_name, 
FROM 
    [project:table] 
GROUP BY 1 

聲明和結果將是

id | last_record_of_name 
---------- 
1 | banana 
2 | carrot 
3 | orange 

這利用了函數的LAST https://cloud.google.com/bigquery/docs/reference/legacy-sql#last

如果在BigQuery中使用標準sql,是否有類似的功能?

回答

4

來自傳統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_AGGORDER BYLIMIT 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    | 
+----+---------------------+ 

查詢的行爲定義明確,並根據您的樣本輸入和輸出提供期望的結果。