2015-09-25 80 views
4

我想從基於時間戳的值列中提取第一個非空值。有人可以分享你的想法。謝謝。如何從Big Query中的一列值中獲取第一個非空值?

到目前爲止,我嘗試了什麼?

FIRST_VALUE(column) OVER (PARTITION BY id ORDER BY timestamp) 

Input :-

id,column,timestamp 
1,NULL,10:30 am 
1,NULL,10:31 am 
1,'xyz',10:32 am 
1,'def',10:33 am 
2,NULL,11:30 am 
2,'abc',11:31 am 

Output(expected) :- 
1,'xyz',10:30 am 
1,'xyz',10:31 am 
1,'xyz',10:32 am 
1,'xyz',10:33 am 
2,'abc',11:30 am 
2,'abc',11:31 am 
+0

最初的聲明和您的示例輸出似乎不一致。看起來你想用第一個非'NULL'值填充NULL值。 –

+0

不需要..我需要將第一個非空值作爲col級別中所有值的輸出。 – Teja

回答

2

據我所知,大查詢像 'IGNORE NULLS' 或 'NULLS LAST' 任何選項。鑑於此,這是我能想到的最簡單的解決方案。我希望看到更簡單的解決方案。 假設輸入數據爲表「original_data」,

select w2.id, w1.column, w2.timestamp 
from 
(select id,column,timestamp 
    from 
    (select id,column,timestamp, row_number() 
        over (partition BY id ORDER BY timestamp) position 
     FROM original_data 
     where column is not null 
    ) 
    where position=1 
) w1 
right outer join 
original_data as w2 
on w1.id = w2.id 
+0

快速更新:現在支持使用「IGNORE NULLS」的可能性:https://cloud.google.com/bigquery/docs/release-notes#november_2_2017 – Sourygna

4

嘗試字符串操作這個老把戲:

Select 
ID, 
    Column, 
    ttimestamp, 
    LTRIM(Right(CColumn,20)) as CColumn, 
    FROM 
(SELECT 
    ID, 
    Column, 
    ttimestamp, 
    MIN(Concat(RPAD(IF(Column is null, '9999999999999999',STRING(ttimestamp)),20,'0'),LPAD(Column,20,' '))) OVER (Partition by ID) CColumn 
FROM (

    SELECT 
    * 
    FROM (Select 1 as ID, STRING(NULL) as Column, 0.4375 as ttimestamp), 
     (Select 1 as ID, STRING(NULL) as Column, 0.438194444444444 as ttimestamp), 
     (Select 1 as ID, 'xyz' as Column, 0.438888888888889 as ttimestamp), 
     (Select 1 as ID, 'def' as Column, 0.439583333333333 as ttimestamp), 
     (Select 2 as ID, STRING(NULL) as Column, 0.479166666666667 as ttimestamp), 
     (Select 2 as ID, 'abc' as Column, 0.479861111111111 as ttimestamp) 
)) 
2

您可以修改你這樣的SQL得到你想要的數據。

FIRST_VALUE(column) 
    OVER ( 
    PARTITION BY id 
    ORDER BY 
     CASE WHEN column IS NULL then 0 ELSE 1 END DESC, 
     timestamp 
) 
+0

MikeD確定此查詢有效嗎?我正在嘗試這個,我得到錯誤信息:「在解析表達式中,ORDER BY必須引用命名列。找到CASE」 – goRunToStack

0

SELECT標識,
(SELECT頂(1)從TEST1柱其中id = 1和列不爲空,以便通過自動識別降序)作爲名稱 ,時間戳 FROM yourTable

輸出: - 1,'xyz',10:30 am 1,'xyz',10:31 am 1,'xyz',10:32 am 1,'xyz',10:33 am 2'abc' ,11:30 am 2,'abc',11:31 am

相關問題