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;
2
Answers
You can get the expected output by changing query as in this demo.
Output :
The proper solution here is to do a 2 level aggregation. First, we can aggregate by both
ID
andStuId
to determine whichStuId
have only falseIsDone
values. Then we aggregate byID
alone to get the pure false counts.