Created
July 6, 2017 08:07
-
-
Save drorata/bdfa45d1b8076daf493063838939decf to your computer and use it in GitHub Desktop.
Count positives and negatives of a value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- A query that count the number of positive/zero/negatives of the column 'value' | |
| --- and group it by the year-month | |
| WITH t AS | |
| ( SELECT value, | |
| YEAR, | |
| MONTH | |
| FROM my_table AS l | |
| ) | |
| SELECT YEAR, | |
| MONTH, | |
| sum(CASE | |
| WHEN value > 0 THEN 1 | |
| ELSE 0 | |
| END) AS positive, | |
| sum(CASE | |
| WHEN value = 0 THEN 1 | |
| ELSE 0 | |
| END) AS zero, | |
| sum(CASE | |
| WHEN value < 0 THEN 1 | |
| ELSE 0 | |
| END) AS negative | |
| FROM t | |
| GROUP BY t.YEAR, | |
| t.MONTH | |
| ORDER BY YEAR, | |
| MONTH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment