Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery IF field exists THEN

I'm running a query over multiple tables unioned together over a particular time range.

In the past, a particular field did not exist in the "schema" but about halfway through that time range, the field began its existence and started getting populated with data.

Is there a way to conditionally select it if it exists, else arbitrarily populate a named field with a value?

like this:

SELECT
  (CASE WHEN exists(my_field) THEN my_field ELSE "0" END) as "my_field"
FROM <somewhere>
like image 414
Kristian Avatar asked Aug 28 '15 17:08

Kristian


1 Answers

Below should give you direction

SELECT * FROM
(SELECT * FROM <somewhere w/o my_field>),
(SELECT * FROM <somewhere with my_field>)

Assuming you have a, b and c as a fields in your original table () - above can be used (see below) if you need to change missing values from NULL to 0:

SELECT a, b, c, COALESCE(my_field, 0) as my_field
FROM
(SELECT * FROM <somewhere w/o my_field>),
(SELECT * FROM <somewhere with my_field>)
like image 161
Mikhail Berlyant Avatar answered Oct 22 '22 00:10

Mikhail Berlyant