SQL查询的简写,用于在许多表中按天计算记录

时间:2018-06-18 15:45:10

标签: sql google-bigquery

我正在尝试创建一个表来验证数据集中许多不同表中的数据。我使用以下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),

...

2 个答案:

答案 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]