How can I count rows that meet a condition inside each group?
Quick answer
In SQLite, group attendance rows by person and calculate both COUNT(*) and COUNT(*) FILTER (WHERE status = 'attended'). The first counts every row in each group; the filter limits the second count to rows marked attended. [S1] The result includes groups represented in the attendance rows. Adjust the table, column, and status names to match your data.
The question, the details, the next step.
A practical checklist
Confirm the table has one row for each session you intend to count.
Replace
person,status, andattendancewith your actual column and table names.Use the exact stored value for attended sessions in the filter condition.
Decide whether people without attendance rows need to appear; the query groups rows present in
attendance.Compare the output with a few example rows to confirm the grouping and status condition match your intent.
A few more useful answers
Does the total include missed sessions?
Yes. COUNT(*) counts every row in each person's group, regardless of status. [S1]
Will this show a person who has no attendance rows?
No. Grouping the attendance rows produces groups for people represented in those rows. Including people with no attendance rows requires starting from a separate list of people and combining it with the attendance data.
Sources
References used for this guide.
- [S1] SQLite aggregate FILTER clause and row counts sqlite.org · Checked
