We have 2 columns in the first Site_ID (any, but repeated), in the second Level (0 or 1)

well, an example
123.0
666.1
967.0
666.0

In the end, I want to get (Site_id / count (Level = 0) / count (Level = 1)), that is, from the example above, we get

123,1,0
666,1,1
967,1,0

    2 answers 2

    Need to use CASE inside COUNT

     SELECT Site_id, COUNT(CASE Level WHEN 0 THEN 1 END) AS Level_0, COUNT(CASE Level WHEN 1 THEN 1 END) AS Level_1 FROM Table GROUP BY Site_id; 
       SELECT Site_id, SUM(Level = 0) AS Level_0, SUM(Level = 1) AS Level_1 FROM DataTable GROUP BY Site_id; 
      • Please describe the idea of ​​how the SUM(Level = 0) construction works - mymedia
      • @mymedia In strict accordance with the documentation: the values TRUE and FALSE are merely aliases for 1 and 0, respectively . - Akina