Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery: Group by table name

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.

like image 864
mattes Avatar asked Feb 19 '14 14:02

mattes


People also ask

What is wildcard table?

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.

What is array AGG in BigQuery?

Definition. The ARRAY_AGG function in BigQuery creates an ARRAY from another expression or table. It is basically the opposite of UNNEST.

What is nested table in BigQuery?

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.

What is Backticks in BigQuery?

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.


1 Answers

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;
like image 143
Jordan Tigani Avatar answered Nov 13 '22 17:11

Jordan Tigani