Sorry for the naff title, but I’m not really sure how to explain this, I’m one of the new generation whose SQL skills have degraded thanks to the active record patterns!
Basically I have three tables in PostgreSQL
Client (One Client has many maps)
– id
Maps (Map has one client and many layers)
– id
– client_id
Layer (Layer has one map)
– id
– map_id
I would like to write an SQL query that returns Cliend.id along with a count of how many maps that client has and the total number of layers the client has across all maps.
Is this possible with a single query? Speed isn’t of concern as this is just for analytical purposes so will be run infrequently.
I’d use a pair of subqueries for this. Something like: