Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have an attendance table with one row per person's session. How can I show each person's total recorded sessions and how many they attended?

Geez

In SQLite, group by the person identifier and put the condition inside the attended count. Assuming the table is named attendance and has columns person and status, use:

SELECT
  person,
  COUNT(*) AS total_sessions,
  COUNT(*) FILTER (WHERE status = 'attended') AS attended_sessions
FROM attendance
GROUP BY person
ORDER BY person;

COUNT(*) counts all rows in each group. The FILTER clause makes the other aggregate count only rows where its condition is true. [S1] The status text must match the values in your table; if yours uses a different label, replace 'attended' with that exact value. The aliases give the two results clear column names.

Follow-up question

Can you show how the query works with a small example?

Geez

Assume these are the recorded rows:

person | status
Ari    | attended
Ari    | missed
Ari    | attended
Bo     | missed
Bo     | attended

The query returns:

person | total_sessions | attended_sessions
Ari    | 3              | 2
Bo     | 2              | 1

For Ari, the group has three rows, so the total is 3; two rows satisfy the attended condition, so that count is 2. Bo's group has two rows, and one satisfies the condition. These are illustrative values based only on the assumed rows. The query groups by person, so it calculates the two counts separately for each person rather than producing one count for the whole table.

Follow-up question

What if I want people with zero attendance records to appear too?

Geez

This query groups the rows in attendance, so its results include people represented in those rows. A person with no attendance row will not have a group in that input to count. To include everyone, you would need to begin with a separate list of people and combine it with the attendance data; the exact query depends on your table names and keys. The supplied aggregate behavior establishes the counts within groups, but not the details of that separate-table setup. [S1] If you only need counts for people who have recorded sessions, the query as written is sufficient.

Follow-up question

How should I adapt this if my database doesn't accept FILTER?

Geez

The FILTER form shown here is documented for SQLite; the available evidence does not establish equivalent syntax or version availability in other database products. [S1] Check the documentation for your database and version before changing the query. In particular, confirm how that system expresses a condition within an aggregate, then keep the same grouping and output goals: one total count per person and one count limited to attended rows. Do not assume the SQLite syntax will work unchanged elsewhere.

A practical checklist

  • Confirm the table has one row for each session you intend to count.

  • Replace person, status, and attendance with 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.

  1. [S1] SQLite aggregate FILTER clause and row counts
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work