0

I would like to do something like this in BQ. The tables have different schemas. Any ideas how to achieve this?

SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID, Withdrawal_this_hour FROM `tb1`
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID, Refill_this_hour FROM `tb2` 
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID, changes_this_hour, net_amount, date from `tb3`

Thanks in advance..

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
i_am_cris
  • 557
  • 1
  • 5
  • 19

2 Answers2

2

The columns have to be the same, so something like this:

SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
       Withdrawal_this_hour, NULL as Refill_this_hour,
       NULL as changes_this_hour, NULL as net_amount, NULL as date 
FROM `tb1`
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
       NULL, Refill_this_hour, NULL, NULL, NULL
FROM `tb2` 
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
       NULL, NULL, changes_this_hour, net_amount, date 
FROM `tb3`
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

For union all number of column and their data have to be same for all the tables big query docs you could try like below by using cte

  with cte as  (

   SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
   Withdrawal_this_hour, NULL as Refill_this_hour,
   NULL as changes_this_hour, NULL as net_amount, NULL as date
  UNION ALL
  SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
   NULL, Refill_this_hour, NULL, NULL, NULL FROM tb2
   UNION ALL
  SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
   NULL, NULL, changes_this_hour, net_amount, date 
  FROM tb3
 ) select * from cte 
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63