Let's say I have two existing tables, "dogs" and "cats":
dog_name | owner ---------+------ Sparky | Bob Rover | Bob Snoopy | Chuck Odie | Jon cat_name | owner ---------+------ Garfield | Jon Muffy | Sam Stupid | Bob
How do I write a query with this output?
owner | num_dogs | num_cats ------+----------+--------- Bob | 2 | 1 Chuck | 1 | 0 Sam | 0 | 1 Jon | 1 | 1
The answer I've got is the next query
select owner, sum(num_dogs), sum(num_cats) from
(select owner, 1 as num_dogs, 0 as num_cats from dogs
union
select owner, 0 as num_dogs, 1 as num_cats from cats)
group by owner
but the problem is that sql doesn't support "select * from (select...)" type of query
Instead, there has to be a table name after "from".
Unfortunately I can't afford myself to have temporary table or a table created with "select into" clause. I need some workaround for the proposed solution where a nested select clause is formed in this way.
What's your opinion?
Cheers