Sql-server – How to count more than one time with different conditions

group byquerysql serversql-server-2016

Having a table like this:

Example table

I want to get a result like this:

Result

Something like:

SELECT ParentId, COUNT(SomeValue = 1) [CountMin], COUNT(SomeValue > 1) [CountRest]
FROM MyTable
GROUP BY ParentId

Best Answer

You can use SUM(CASE...)

SELECT
    ParentID,
    SUM(CASE WHEN SomeValue = 1 THEN 1 ELSE 0 END) AS CountMin,
    SUM(CASE WHEN SomeValue <> 1 THEN 1 ELSE 0 END) AS CountRest
FROM
    MyTable
WHERE
    ParentID = 123;