2 years ago

#38311

test-img

Laura Choi

CASE STATEMENT + COUNT function not working - SQL Big Query

when i run this code it does not seem to be working in big query...

SELECT date,
      COUNT(CASE WHEN product = "fan" AND product color = "white" 
                 THEN product_code ELSE "null" END) AS number_of_whitefans, 
  FROM `radiant-oven-328313.customer_data.customer_purchase` 
 GROUP by date

Been trying to run this code many times and i wonder if it's any commas i've missed or just the order of the syntax? i'm running this using bigquery - should i have included a else statement? I've been following the below line of code logic from datacamp so could someone tell me if i've replicated the same thing for my code above with a different data set?

SELECT season, 
       COUNT(CASE WHEN hometeam_id = 8650 
                       AND home_goal > away_goal
                  THEN match_id END) AS home_wins
       COUNT(CASE WHEN awayteam_id = 8650 
                       AND away_goal > home_goal THEN id END) AS away_wins 
  From match 
 GROUP by season;

sql

count

google-bigquery

case-statement

0 Answers

Your Answer

Accepted video resources