Skip to content
Advertisement

In PostgreSQL, how to query for a computed results by the grouping of a field?

In my PostgreSQL database I have an invitations table like so:

Invitations Table:

Fields Details

I’d like to query the Invitations table and group by unique sources. And then per source, include the total # of records and the total of those completed. Completed being where complated_at is NOT NULL.

Desired Results:

Suggestions?

Advertisement

Answer

You can do aggregation like so:

count(*) counts all records in the group, while count(completed_at) counts only records whose completed_at is not null.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement