Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all

How to merge these query
SQL
select count(a.pk_id) from test a
union
select count(b.pk_id) from usertesting b
union
select count(c.user_id) from user1 c;

means wants to write in single query
i tried
SQL
select count(a.pk_id),count(b.pk_id),count(c.user_id) from test a ,usertesting b, user1 c;

but this is not showing the true result and also tried using different joining.

Unable to write this above query in a single query

Please help me to write the above query in a single query

Thanks in advance.
Posted

The reason your second query is not showing a true result is because you're joining the tables without any kind on link. That means that the counts will be multiplied by the other tables. test * usertesting * user1.

So if you had the following tables:

TableA, TableB, TableC
1       6       a
2       7       b
3       8       c


By selecting as you do in the second query you'd get the following results:

AValue, BValue, CValue
1       6       a
1       6       b
1       6       c
1       7       a
1       7       b
1       7       c
1       8       a
1       8       b
1       8       c
2       6       a
2       6       b
2       6       c
2       7       a
2       7       b
2       7       c
2       8       a
2       8       b
2       8       c
3       6       a
3       6       b
3       6       c
3       7       a
3       7       b
3       7       c
3       8       a
3       8       b
3       8       c


Personally I think the union select is the best way to get the data you're after but the following should achieve your desired result.

SQL
SELECT cTest, cUserTesting, cUser1 FROM
(SELECT cTest = COUNT(pk_id) FROM test),
(SELECT cUserTesting = COUNT(pk_id) FROM usertesting),
(SELECT cUser1 = COUNT(user_id) FROM user1)


As each of the tables listed only has one row there won't be a multiplication of results.

Although if your problem with the union is you don't know which count is which you could try the following.

C#
select cType = 'test', vCount = count(a.pk_id) from test a
union
select 'usertesting', count(b.pk_id) from usertesting b
union
select 'user1', count(c.user_id) from user1 c;
 
Share this answer
 
v2
Comments
Arunprasath Natarajan 14-Jun-12 5:02am    
5
Maciej Los 14-Jun-12 12:46pm    
Good explonations, +5!
You can do it in this way, using variables:
SQL
DECLARE @c1 INT
DECLARE @c2 INT
DECLARE @c3 INT

select @c1 = count(a.pk_id) from test a
select @c2 = count(b.pk_id) from usertesting b
select @c3 = count(c.user_id) from user1 c;

SELECT @c1 AS [aPk_id], @c2 AS [bPk_id], @c3 AS [cUs_id]
 
Share this answer
 
Comments
Sandeep Mewara 14-Jun-12 16:33pm    
Why 1vote... 5!
Maciej Los 14-Jun-12 17:47pm    
Vote of 1 - I'm wondering too...
Thank you, Sandeep ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900