Search code examples
sqlcountifdimension

Count number of + and - values based on another column


I am trying to get a simple count of all the negative and positive values of a specific column on my database. I want to be able to count these values based on another column. What would be the best way to go about handling this problem.

enter image description here


Solution

  • Something like this:

    SELECT 
      SUM(CASE WHEN column > 0 THEN 1 ELSE 0 END) as count_positive,
      SUM(CASE WHEN column < 0 THEN 1 ELSE 0 END) as count_negative
    FROM
      table
    

    You must put the column name, table name, and decide whether 0 is positive, negative or excluded (my choice)