SQL: Combine Select count(*) from multiple tables
How do you combine multiple select count(*) from different table into one return?
I have a similar sitiuation as this post
but I want one return.
I tried Union all but it spit back 3 separate rows of count. How do you combine them into one?
select count(*) from foo1 where ID = '00123244552000258'
union all
select count(*) from foo2 where ID = '00123244552000258'
union all
select count(*) from foo3 where ID = '00123244552000258'
edit: I'm on MS SQL 2005
Solution 1:
SELECT
(select count(*) from foo1 where ID = '00123244552000258')
+
(select count(*) from foo2 where ID = '00123244552000258')
+
(select count(*) from foo3 where ID = '00123244552000258')
This is an easy way.
Solution 2:
I'm surprised no one has suggested this variation:
SELECT SUM(c)
FROM (
SELECT COUNT(*) AS c FROM foo1 WHERE ID = '00123244552000258'
UNION ALL
SELECT COUNT(*) FROM foo2 WHERE ID = '00123244552000258'
UNION ALL
SELECT COUNT(*) FROM foo3 WHERE ID = '00123244552000258'
);
Solution 3:
select
(select count(*) from foo) as foo
, (select count(*) from bar) as bar
, ...