gpt4 book ai didi

google-bigquery - 从同一个表中取消嵌套多个字段 - BigQuery

转载 作者:行者123 更新时间:2023-12-04 23:37:33 26 4
gpt4 key购买 nike

我想为以下查询同时取消 Info2 和 Info3。如何对 BigQuery 中的同一个表执行此操作。 Info3 也有一个类似于 info2 的条件。

谢谢!

ID | startDate | endDate | info1 | info2 | info 3  
----------------------------------------------------
1 | 11-12-2000 | 11-12-2010 | Blue | Circle | A
| Triangle | B
| Square |
----------------------------------------------------
2 | 11-12-2001 | 11-12-2011 | Yellow | <*> | C
----------------------------------------------------
3 | 11-12-2007 | 11-12-2008 | Brown | Circle | D
| Triangle | B
----------------------------------------------------

info2!="<*>"AND inf3=B
ID | startDate | endDate | info1 | info2 | info 3  
----------------------------------------------------
1 | 11-12-2000 | 11-12-2010 | Blue | Circle | B
1 | 11-12-2000 | 11-12-2010 | Blue | Triangle | B
1 | 11-12-2000 | 11-12-2010 | Blue | Square | B
----------------------------------------------------
3 | 11-12-2007 | 11-12-2008 | Brown | Circle | B
3 | 11-12-2007 | 11-12-2008 | Brown | Triangle | B
----------------------------------------------------

以下查询将在没有 Info3 的情况下工作。
#standard_SQL    
SELECT
id,
startDate,
endDate,
info1,
info2,
info3
FROM
`MY_DB`
WHERE
EXISTS (
SELECT
Info2
FROM
UNNEST(Info2) Temp
WHERE
Temp NOT IN ("<*>"))

最佳答案

下面是 BigQuery 标准 SQL



#standardSQL
WITH `project.dataset.MY_DB` AS (
SELECT 1 id, '11-12-2000' startDate, '11-12-2010' endDate, 'Blue' info1, ['Circle','Triangle', 'Square'] info2, ['A', 'B'] info3 UNION ALL
SELECT 2, '11-12-2001', '11-12-2011', 'Yellow', ['<*>'], ['C'] UNION ALL
SELECT 3, '11-12-2007', '11-12-2008', 'Brown', ['Circle','Triangle'], ['D', 'B']
)
SELECT id, startDate, endDate, info1, info2, info3
FROM `project.dataset.MY_DB`, UNNEST(info2) info2, UNNEST(info3) info3
WHERE info2 != '<*>' AND info3 = 'B'
ORDER BY id

结果如下

Row id  startDate   endDate     info1   info2       info3    
1 1 11-12-2000 11-12-2010 Blue Circle B
2 1 11-12-2000 11-12-2010 Blue Triangle B
3 1 11-12-2000 11-12-2010 Blue Square B
4 3 11-12-2007 11-12-2008 Brown Circle B
5 3 11-12-2007 11-12-2008 Brown Triangle B

关于google-bigquery - 从同一个表中取消嵌套多个字段 - BigQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918378/

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