문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top