Reputation: 43
I'm trying to do this query in BigQuery.
I have a Table1, and a Table 2. I'd like to do something like this:
If (select Table 2 with specific criteria) returns > 0 records,
then perform this select on table 1 (select one),
Otherwise, perform another select on table 1 (select 2).
I can do something like:
SELECT IF (1=1,"YES", "NO"), but, when I replace 1=1 with my select, like this:
SELECT IF ((SELECT count(*) FROM Table1)>0,"YES", "NO")
is not working anymore...
Any help??? I hope any expert can help me!
best regards,
Albert
Upvotes: 1
Views: 74
Reputation: 708
You can't have nested select statement like that. What needs to happen is your inner select has to be a subquery and aliased something like:
SELECT
IF(inner_table.count_column > 0, "YES", "NO) -- here we select the 'count_column' from 'inner_table
FROM
(
(SELECT count(*) as count_column FROM table1)
) as inner_table -- aliasing the inner select as 'inner_table'
Hope this makes sense. Pretty much this is how it works with bigquery, you need to construct your nested statements into subqueries. Here's some reading https://developers.google.com/bigquery/query-reference#from
Upvotes: 3