gpt4 book ai didi

sql - 如何合并 BigQuery 中多行的 NULL?

转载 作者:行者123 更新时间:2023-12-05 06:21:38 28 4
gpt4 key购买 nike

我有下表:

Date       |event_number| customer_id1 | customer_age | customer_gender
10/01/2020 | 1 | abc | NULL | NULL
10/01/2020 | 2 | abc | NULL | male
10/01/2020 | 3 | abc | 45 | NULL
10/01/2020 | 1 | def | 30 | NULL

我想每天运行一个 SQL 查询来查找 custom_id1、customer_age、customer_gender 的新组合。

输出应该是这样的:

query_run_time | customer_id1 | customer_age | customer gender
11/01/2020 | abc | 45 | male
11/01/2020 | def | 30 | NULL

查询运行时间是查询运行的日期。如果组合(customer_id、custmer_age、customer_gender)已经在表中,我不想插入该行。

谢谢

最佳答案

您可以使用窗口函数为合并多个查询分配内部行号,例如像这样:

SELECT COALESCE(a.customer_id, b.customer_id) as customer_id
, customer_age
, customer_gender
FROM (
SELECT customer_id, customer_age
, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY customer_age ) AS row_no
FROM customer_event
WHERE customer_age IS NOT NULL
) a
FULL JOIN (
SELECT customer_id, customer_gender
, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY customer_gender ) AS row_no
FROM customer_event
WHERE customer_gender IS NOT NULL
) b ON b.customer_id = a.customer_id
AND b.row_no = a.row_no
ORDER BY COALESCE(a.customer_id, b.customer_id)
, COALESCE(a.row_no, b.row_no)

架构和测试数据

CREATE TABLE customer_event (
event_number INT NOT NULL,
customer_id VARCHAR(10) NOT NULL,
customer_age INT,
customer_gender VARCHAR(10)
);
INSERT INTO customer_event VALUES
( 1, 'abc', NULL, NULL ),
( 2, 'abc', NULL, 'male' ),
( 3, 'abc', 45 , NULL ),
( 4, 'abc', 50 , 'female' ),
( 5, 'abc', 27 , NULL ),
( 1, 'def', 30 , NULL );

输出

customer_id  customer_age  customer_gender
abc 27 female
abc 45 male
abc 50 (null)
def 30 (null)

以上是在 SQL Fiddle 上使用 PostgreSQL 9.6 进行的测试。 .

关于sql - 如何合并 BigQuery 中多行的 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59695041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com