Select statement pulling from multiple tables

I need some help with a query.

I need a query that will return data in one result.  I am using it like the below:

Select
(select count(*) from table1 where data = 1) as 001,
(select count(*) from table1 where data = 2) as 002,
(select count(*) from table1 where data = 3) as 003


Now, what I have is some of data = 2 is in another table.

So I would need
(select count(*) from table1 where data = 2)  Plus (select count(*) from table2 where data=2)
as 002

Is there a way to do this and have it return the data in one master select statement without using temp tables?
jstorm13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
Select
(select count(*) from table1 where data = 1) as 001,
(  (select count(*) from table1 where data = 2)  + (select count(*) from table2 where data=2) ) as 002,
(select count(*) from table1 where data = 3) as 003
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chapmandewCommented:
Select
sum(case when data = 1 then 1 else 0 end) as [001],
sum(case when data = 2 then 1 else 0 end) as [002],
sum(case when data = 3 then 1 else 0 end) as [003]
from table1
where data in(1,2,3)
0
chapmandewCommented:
No reason to hit the table 3 times in the same query...

if you need to add them, do this

Select
sum(case when data = 1 then 1 else 0 end) +
sum(case when data = 2 then 1 else 0 end) +
sum(case when data = 3 then 1 else 0 end)
from table1
where data in(1,2,3)
0
jstorm13Author Commented:
Thank you!  
0
chapmandewCommented:
For the record, hitting the table 3 times is a BAD way to do this....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.