Join BigQuery连接和填补空白
我有以下要合并的数据集,但我正在尝试填充空行Join BigQuery连接和填补空白,join,google-bigquery,Join,Google Bigquery,我有以下要合并的数据集,但我正在尝试填充空行 WITH data1 as ( SELECT "2021-01-01" date, 'abc' company, 3 cumulative_count UNION ALL SELECT "2021-01-02" date, 'abc' company, 4 cumulative_count UNION ALL SELECT "2021-01-06" date, 'abc' com
WITH data1 as (
SELECT "2021-01-01" date, 'abc' company, 3 cumulative_count UNION ALL
SELECT "2021-01-02" date, 'abc' company, 4 cumulative_count UNION ALL
SELECT "2021-01-06" date, 'abc' company, 17 cumulative_count UNION ALL
SELECT "2021-01-02" date , 'xzy' company, 2 cumulative_count UNION ALL
SELECT "2021-01-04" date , 'xzy' company, 16 cumulative_count UNION ALL
SELECT "2021-01-08" date , 'xzy' company, 16 cumulative_count
),
data_dates as (
# SELECT *
# FROM UNNEST(GENERATE_DATE_ARRAY('2021-01-01', '2021-01-08',INTERVAL 1 DAY)) AS date
SELECT "2021-01-01" date UNION ALL
SELECT "2021-01-02" date UNION ALL
SELECT "2021-01-03" date UNION ALL
SELECT "2021-01-04" date UNION ALL
SELECT "2021-01-05" date UNION ALL
SELECT "2021-01-06" date UNION ALL
SELECT "2021-01-07" date UNION ALL
SELECT "2021-01-08" date
)
SELECT
a.date,
b.company,
b.cumulative_count
FROM data_dates as a
LEFT OUTER JOIN data1 as b
ON a.date = b.date
导致
我将如何从2021-01-02复制两家公司2021-01-03行的结果。基本上填充空行,直到新的更新值发挥作用。
另外,我将如何指定2021-01-01的xzy公司。
谢谢。下面就可以了
WITH data1 as (
SELECT "2021-01-01" date, 'abc' company, 3 cumulative_count UNION ALL
SELECT "2021-01-02" date, 'abc' company, 4 cumulative_count UNION ALL
SELECT "2021-01-06" date, 'abc' company, 17 cumulative_count UNION ALL
SELECT "2021-01-02" date , 'xzy' company, 2 cumulative_count UNION ALL
SELECT "2021-01-04" date , 'xzy' company, 16 cumulative_count UNION ALL
SELECT "2021-01-08" date , 'xzy' company, 16 cumulative_count
), data_dates as (
# SELECT *
# FROM UNNEST(GENERATE_DATE_ARRAY('2021-01-01', '2021-01-08',INTERVAL 1 DAY)) AS date
SELECT "2021-01-01" date UNION ALL
SELECT "2021-01-02" date UNION ALL
SELECT "2021-01-03" date UNION ALL
SELECT "2021-01-04" date UNION ALL
SELECT "2021-01-05" date UNION ALL
SELECT "2021-01-06" date UNION ALL
SELECT "2021-01-07" date UNION ALL
SELECT "2021-01-08" date
), data_companies as (
SELECT DISTINCT company
FROM data1
)
SELECT
a.date,
c.company,
IFNULL(cumulative_count, LAST_VALUE(cumulative_count IGNORE NULLS) OVER(PARTITION BY c.company ORDER BY a.date)) cumulative_count
FROM data_dates as a, data_companies as c
LEFT JOIN data1 as b
ON a.date = b.date
AND c.company = b.company
ORDER BY date, company
有输出