Sana
Sana

Reputation: 563

Bigquery if condition for selecting reference table

I have two tables : Table_A and Table_B I also have a parameter {{param.age}}

I would like to check the age, if it is 15 select * from Table_A otherwise select * from table_B.

I came up with this solution which is not working:

SELECT
  CASE 3
    WHEN 3 THEN ( SELECT * FROM `Table_A`)
  ELSE
  (SELECT * FROM `Table_B`)
END

I get this error: scalar subquery cannot have more than one column ... I am sure there is an easy straightforward solution for this which I can not find.

Sudo code:

If parameter = x
Then SELECT * FROM Table_A
Else SELECT * FROM Table_B

Upvotes: 0

Views: 506

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269543

Assuming the two queries have the same columns, you can do:

select *
from table_A
where parameter = x
union all
select *
from table_B
where parameter <> x;

If the two tables do not have the same columns, I would question what you want to do. A SQL query returns a fixed set of columns. Which columns do you want? If the common columns between the two tables, then select them explicitly.

Upvotes: 2

Related Questions