匹配SQL/BigQuery中相同索引的数组元素 [英] Match array elements of the same index in SQL / BigQuery
本文介绍了匹配SQL/BigQuery中相同索引的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在数组中输出一个元素,该元素与SQL/BigQuery中的前一个匹配期间的索引匹配
例如,我有以下5个字段。Match_Home_Date和Match_Away_Date采用数组(字符串)格式。
Match_ID | Match_Home | Match_away | Match_Home_Date | Match_Away_Date |
---|---|---|---|---|
121 | 101、102 | 103、121 | [01-02-2021,01-05-2021] | [01-07-2021,01-09-2021] |
131 | 131、140 | 117、115 | [02-02-2021,02-15-2021] | [02-20-2021,02-25-2021] |
我要输出一个最终日期字段,当Match_ID与Match_Home或Match_Away匹配时,它将输出相同索引的相应Match_Home_Date或Match_Away_Date。
输出应为:
Final Date
01-09-2021
02-02-2021
推荐答案
考虑以下方法
select Match_ID,
(Match_Home_Date || Match_Away_Date)[offset(
( select offset
from unnest(split(Match_Home) || split(Match_Away)) id with offset
where trim(id) = '' || Match_ID
))] as Final_Date
from your_table
如果应用于问题中的样本数据-输出为
您可以使用下面的CTE测试上面的
with your_table as (
select 121 Match_ID, '101, 102' Match_Home, '103, 121' Match_Away, ['01-02-2021', '01-05-2021'] Match_Home_Date, ['01-07-2021', '01-09-2021'] Match_Away_Date union all
select 131, '131, 140', '117, 115', ['02-02-2021', '02-15-2021'], ['02-20-2021', '02-25-2021']
)
这篇关于匹配SQL/BigQuery中相同索引的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文