Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional CASE WHEN select snowflake SQL

I am stuck on a conditional snowflake select sql. I am trying to count the IDs when they have the corresponding categorial value. I would appreciate some help.

Thanks

SELECT 
YEAR(DATETIME) AS YEAR, 
WEEKOVERYEAR(DATETIME) AS WEEK,
COUNT(CASE WHEN ID THEN CATEGORY = 'A')
from table 
group by week, year;

like image 320
B. Choi Avatar asked Jul 30 '26 02:07

B. Choi


1 Answers

Here is one method:

SELECT YEAR(DATETIME) AS YEAR, 
       WEEKOVERYEAR(DATETIME) AS WEEK,
       SUM(CASE WHEN CATEGORY = 'A' THEN 1 ELSE 0 END) as num_a
FROM table 
GROUP BY week, year;
like image 120
Gordon Linoff Avatar answered Aug 01 '26 16:08

Gordon Linoff