BigQuery - combine tables
Asked Answered
S

3

8

I have monthly Data in BigQuery but I want to create a yearly database, meaning combine the 12 subdatabase to 1.

How can it be done?

The structure is identical for all 12 databases in form:

Date, Name, Amount, Value, Type_of_Good

I thought JOIN might help me, but it is not the case.

Thanks

Springe answered 11/1, 2015 at 11:27 Comment(0)
L
8

you can use the following syntax

SELECT Date, Name, Amount, Value, Type_of_Good
FROM
(select Date, Name, Amount, Value, Type_of_Good from january ...),
(select Date, Name, Amount, Value, Type_of_Good from february ...),
...
(select Date, Name, Amount, Value, Type_of_Good from december ...)
Laritalariviere answered 11/1, 2015 at 16:53 Comment(1)
A simpler one: SELECT Date, Name, Amount, Value, Type_of_Good FROM [january],[february],[march]Nganngc
C
7

Pentium10's suggestion works, but you might also consider two other options:

  1. Use TABLE_QUERY() (described here) which will allow you to construct a query that selects from multiple tables.
  2. Use a view (described here). Note that views can't currently be used with TABLE_QUERY or TABLE_DATE_RANGE (although that functionality should be coming soon!). But a view would allow you to take the query suggested by Pentium10 and save it so that it looks like a single table.
  3. Use table copy with a write disposition of append to copy the individual tables into your year summary table. While this would mean that you would get charged for storage for the new table, it would also let you delete the old tables if you don't need them any more, and be the most flexible option since you then have a real table with the combined data.
Chariness answered 11/1, 2015 at 18:35 Comment(0)
S
0

You can also use a UNION ALL to concatenate tables with identical columns.

SELECT 
    Date, Name, Amount, Value, Type_of_Good
FROM
    january
UNION ALL
SELECT
    Date, Name, Amount, Value, Type_of_Good
FROM
    february

And you can UNION ALL for all the months you need.

Sverige answered 24/2, 2022 at 15:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.