简体   繁体   中英

Shorthand for SQL query to count records by day across many tables

I am trying to create a table to validate my data across many different tables within a dataset. I use the following SQL query to do this, but is there any way to save time copying in the names of all tables within my dataset, either in the top level SELECT statement or the sub queries?

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),

...

You can also use 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;
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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM