In my PostgreSQL database I have an invitations table like so:
Invitations Table:
x
id, created_at, source, completed_atFields Details
id: integercreated_at: timestampsource: varcharcompleted_at: timestampI’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:
columns: source, total, total_completedsource_a | 100 | 50source_b | 200 | 33source_c | 301 | 3Suggestions?
Advertisement
Answer
You can do aggregation like so:
select source, count(*) total, count(completed_at) total_completedfrom invitationsgroup by sourcecount(*) counts all records in the group, while count(completed_at) counts only records whose completed_at is not null.