簡體   English   中英

BigQuery - 根據 first() 非 null 值從不同列中獲取值

[英]BigQuery - get values from different columns based on first() non null value

給定一張桌子

session_id article article_type n_page
1          null    null         1
1          abc     mag          2
1          abb     food         3
2          agg     food         1
2          hag     mag          2

我需要按 session_id 分組並從文章中獲取第一個非 null 值,並與其他列結合為:

session_id first_article article_type n_page
1          abc           mag          2
2          agg           food         1

以及最后一個值:

session_id first_article article_type n_page
1          abb           food         3
2          hag           mag          2

我試過這個:

SELECT session_id,first_value(article_id ignore nulls) over(partition by session_id order by event_time ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) a,
last_value(article_id ignore nulls) over(partition by session_id order by event_time ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) b
FROM Table

但它沒有給我“n_page”,我在創建 session_id 時創建了 n_page,但可以即時生成它謝謝!

相關子查詢怎么樣?

select t.*
from t
where t.n_page = (select min(t2.n_page)
                  from t t2
                  where t2.session_id = t.session_id and t2.article is not null
                 );

然后,您可以將min()替換為max()以進行第二個查詢。

以下是 BigQuery 標准 SQL

對於第一個非 NULL 文章

#standardSQL
SELECT AS VALUE ARRAY_AGG(
    STRUCT(session_id, article AS first_article, article_type, n_page) 
    ORDER BY n_page LIMIT 1
  )[OFFSET(0)] 
FROM `project.dataset.table`
WHERE NOT article IS NULL
GROUP BY session_id  

最后非 NULL 文章

#standardSQL
SELECT AS VALUE ARRAY_AGG(
    STRUCT(session_id, article AS last_article, article_type, n_page) 
    ORDER BY n_page DESC LIMIT 1
  )[OFFSET(0)] 
FROM `project.dataset.table`
WHERE NOT article IS NULL
GROUP BY session_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM