Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sql count distinct by col and sum false and true

How to query result as Expect Count with Sqlstatement

ID      StuId    IsDone    ExpectCount
11111   q-01     false        1         
11111   q-02     false        2      
11111   q-03     false        3        
11111   q-02     true         2   
11111   q-04     false        3   
22222   q-04     false        2
11111   q-01     true         1  
11111   q-01     true         1 

expect if same ID StuId IsDone = true will ignore it.

11111 = 3
22222 = 1

but got

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

11111 = 1
22222 = 1

My query

SELECT ID, count(*)
FROM
(SELECT DISTINCT StuId, ID, IsDone FROM Student s where IsDone = false) stu
group by ID;

>Solution :

You can get the expected output by changing query as in this demo.

SELECT ID, COUNT(DISTINCT StuId) - SUM(CASE WHEN IsDone = true THEN 1 ELSE 0 END) as ExpectedCount
FROM Student
GROUP BY ID;

Output :

id ExpectedCount
11111 3
22222 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading