在BigQuery中联接两个表时出现问题。请参阅所附图片。表1列出了公司备案的截止日期,表2列出了历史股价。过去五年中大多数公司的截止日期是相同的,一年(30/09/2018)是一个周末,因此该日没有股价。因此,如何在联接Table1.date = Table2.date的两个表时从Table2的下一个可用日期获取值。谁能让我知道如何在BigQuery中执行查询?
谢谢, 湿婆神
答案 0 :(得分:0)
在注释部分中澄清后,我能够使用StandardSQL创建查询以实现您的目标。
我使用了内置函数LEAD(),该函数根据分区和/或顺序返回下一行的值。下面是查询:
WITH table1 AS (
SELECT DATE(2015,09,30) as end_date, 20 as share_price UNION ALL
SELECT DATE(2016,09,30) as end_date, 30 as share_price UNION ALL
SELECT DATE(2017,09,30) as end_date, 40 as share_price UNION ALL
SELECT DATE(2018,09,30) as end_date, NULL as share_price UNION ALL
SELECT DATE(2019,09,30) as end_date, 60 as share_price
),
table2 AS(
SELECT DATE(2015,09,30) as date, 20 as share_price UNION ALL
SELECT DATE(2016,09,30) as date, 30 as share_price UNION ALL
SELECT DATE(2017,09,30) as date, 40 as share_price UNION ALL
SELECT DATE(2018,09,30) as date, NULL as share_price UNION ALL
SELECT DATE(2018,10,01) as date, 50 as share_price UNION ALL
SELECT DATE(2019,09,30) as date, 60 as share_price
),
table2_ref AS (
SELECT date,
IF(share_price IS NULL, LEAD(share_price,1) OVER (ORDER BY date), share_price) AS share_price
#this following column is not necessary, it is just to check which null values were modified
,IF(share_price IS NULL, "got the next available value", "kept the value") AS check
FROM table2
ORDER BY date
)
SELECT a.date, a.share_price FROM table2_ref a INNER JOIN table1 b ON a.date = b.end_date
输出,
请注意,我已在 table2_ref 中使用了 check 列,以便跟踪更改了哪些值从空到下一个可用价格。因此,它只是可以在输出中检查的标志,但是您可以在代码中取消显示此列(就像我在上面的输出图像中所做的一样)。