sql query to count column values from multiple tables and sumup

Posted by Parthibansk under Sql Server on 5/24/2014 | Points: 10 | Views : 506 | Status : [Member] | Replies : 2
i need a query to count column values from multiple tables and sum up...


i got username column in three tables.. i need to count the values and sum...




Responses

Posted by: Saranpselvam on: 5/26/2014 [Member] Starter | Points: 25

Up
0
Down
Hi
Please execute the following Query


create table #user1 (userid1 int)
insert into #user1
select 1
union all
select 2

create table #user2 (userid2 int)
insert into #user2
select 1
union all
select 2
union all
select 3
create table #user3 (userid3 int)
insert into #user3
select 1
union all
select 2
union all
select 3
union all
select 4
select count(distinct #user1.userid1) as userid1,count(distinct #user2.userid2) as userid2 ,count(distinct #user3.userid3) as userid3
, count(distinct #user1.userid1)+count(distinct #user2.userid2)+count(distinct #user3.userid3) as totalUser

from #user1,#user2,#user3

drop table #user1
drop table #user2
drop table #user3

Thanks
Sarav

Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kirthiga on: 5/31/2014 [Member] Starter | Points: 25

Up
0
Down
Hi,
Try this query this may help you


select Sum(Cnt)Cnt from
(
select count(*) Cnt from Table1
union
select count(*) Cnt from Table2
union
select count(*) Cnt from Table3
)a




Parthibansk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response