I’m trying to count the number of times a “rating” is above 9. Working in Google BigQuery with StandardSQL
I previously was using a CASE WHEN function but wanted to change it to an IF as there is only one case. Previous code which worked fine:
SELECT COUNT(CASE WHEN (survey_responses.survey_rating >= 9) THEN 1 ELSE NULL END) AS survey_responses_num_positives
Current code
SELECT COUNT(IF(survey_responses.survey_rating >= 9),1,NULL) AS survey_responses_num_positives
The current code spits out the error:
No matching signature for function IF for argument types: BOOL. Supported signature: IF(BOOL, ANY, ANY) at [11:39]
Don’t really understand it as the IF statement is a BOOL value no? Would appreciate any help on this. Thanks!
Advertisement
Answer
You missplaced a parenthesis:
SELECT COUNT(IF(survey_responses.survey_rating >= 9,1,NULL)) AS survey_responses_num_positives
IF
takes 3 arguments, the condition, if true, and else.