我正在尝试创建一个表来验证数据集中许多不同表中的数据。我使用以下SQL查询来执行此操作,但有没有办法在顶级SELECT语句或子查询中节省复制到我的数据集中所有表的名称的时间?
SELECT Date, records_table1, records_table2...
FROM
(SELECT RowDate as Date, Count(RowDate) AS records_table1 FROM [project:dataset.table1] GROUP BY Date),
(SELECT RowDate as Date, Count(RowDate) AS records_table2 FROM [project:dataset.table2] GROUP BY Date),
...
答案 0 :(得分:1)
您还可以使用union all
:
select RowDate, sum(table1) as records_table1, sum(table2) as records_table2
from (select RowDate, 1 as table1, 0 as table2
from [project:dataset.table1]
union all
select RowDate, 0, 1
from [project:dataset.table2]
) t
group by RowDate;
答案 1 :(得分:0)
select a.*, b.records_table2 from
(SELECT RowDate as Date, Count(RowDate) AS records_table1 FROM [project:dataset.table1] GROUP BY Date)a
full join
(SELECT RowDate as Date, Count(RowDate) AS records_table2 FROM [project:dataset.table2] GROUP BY Date)b
on a.[Date]=b.[Date]