I’m trying to find the best way to summarize the data in a table
I have a table Info with fields
id
region_number integer (NOT associated with another table)
member_name string
member_active T/F
Members belong to a region, have a name, and are either active or not.
I’m wondering if there is a single query that will create a table with 3 columns, and as many rows as there are unique region_numbers:
For each unique region_number:
region_number
COUNT of members in that region
COUNT of members in that region with active=TRUE
Suppose I have 50 regions, I see how to do it with 2×50 queries but that surely is not the right approach!
You can always group on several things if you’re prepared to do a tiny bit of post-processing:
This allows you do to one query for all region numbers at the same time. There will be one row for the
Tvalues, one for theF, but only if those are present.If you see a case where you’re doing a lot of queries that differ only in identifiers, that’s something you can usually execute in one shot like this.