My dataset (ds1) looks like the following:
2014_01_01_tableA
2014_01_01_tableB
2014_01_02_tableA
2014_01_02_tableB
The query:
SELECT date, COUNT(1) AS counter
FROM [ds1.2014_01_01_tableA], [ds1.2014_01_01_tableB], [ds1.2014_01_02_tableA], [ds1.2014_01_02_tableB]
GROUP BY date;
Returns:
date, counter
2014-01-01, 100
2014-01-02, 200
Is it somehow possible to group by the table name as well? Something like this maybe? ...
SELECT date, COUNT(1) AS counter, TABLE_NAME() AS table_name
FROM [ds1.2014_01_01_tableA], [ds1.2014_01_01_tableB], [ds1.2014_01_02_tableA], [ds1.2014_01_02_tableB]
GROUP BY table_name, date;
Expected result:
date, counter, table_name
2014-01-01, 50, 2014_01_01_tableA
2014-01-01, 50, 2014_01_01_tableB
2014-01-02, 100, 2014_01_02_tableA
2014-01-02, 100, 2014_01_02_tableB
TABLE_NAME() AS table_name
does not exist, unfortunately.
A wildcard table enables you to query multiple tables using concise SQL statements. A wildcard table represents a union of all the tables that match the wildcard expression. Wildcard tables are available only in Google Standard SQL. For equivalent functionality in legacy SQL, see Table wildcard functions.
Definition. The ARRAY_AGG function in BigQuery creates an ARRAY from another expression or table. It is basically the opposite of UNNEST.
BigQuery Nested Fields are fields linked together like a single entity, just like an object or a struct. Consider the following table: Image Source. The “title” field in the above table is a good example of a BigQuery Nested Field.
Backticks are usually used to select columns and tables from the source database, but these are not compulsory since they don't make any difference and return the same result without using it. For example the following queries will work and return the same result in BigQuery.
Unfortunately, there isn't a way to do this cleanly. I've added an internal feature request.
The ugly way to do it is:
SELECT date, COUNT(1) AS counter, table_name
FROM
(SELECT *, "2014_01_01_tableA" as table_name FROM [ds1.2014_01_01_tableA]),
(SELECT *, "2014_01_01_tableB" as table_name FROM [ds1.2014_01_01_tableB]),
(SELECT *, "2014_01_02_tableA" as table_name FROM [ds1.2014_01_02_tableA]),
(SELECT *, "2014_01_02_tableB" as table_name FROM [ds1.2014_01_02_tableB])
GROUP BY table_name, date;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With