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
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 |