Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combining 2 result sets from 2 queries

Author  Topic 

chaturap
Starting Member

2 Posts

Posted - 2010-03-24 : 02:19:48
Hi,

I need to combine 2 result sets from 2 queries.

Output of query 1,

package_id incoming
---------- --------
1 5
2 4
4 3


Output of query 2,

package_id outgoing
---------- --------
1 2
3 3
5 1


I need to get the following combine result set,

package_id incoming outgoing
---------- -------- --------
1 5 2
2 4
3 3
4 3
5 1


I tried this with union, but it is not giving the expected output.

Appreciate your comments.


Thanks,

Chatu



khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-03-24 : 02:24:30
[code]
select package_id,, incoming = sum(incoming), outgoing = sum(outgoing)
from
(
select package_id, incoming, outgoing = NULL
from query1

union all

select package_id, incoming = NULL, outgoing
from query2
) q
group by package_id
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

chaturap
Starting Member

2 Posts

Posted - 2010-03-24 : 03:29:08
Thanks for the quick reply, but it gives an error to union all.

BTW, there is no need to calculate the sum.

Thing is I have many mobile packages and their incoming rates are in tblincoming table. Their outgoing rates are in a different table called tbloutgoing. I need to just display packeage_id, incoming, outgoing rates in a one result set. Problem is some packages may have only either incoming rate or outgoing rate. As a result combined results set will have null values.

Query 1: SELECT package_id, incoming FROM tblincoming

Query 2: SELECT package_id, outgoing FROM tbloutgoing

I just need to display package_id, incoming, outgoing columns without duplications in a one result set.

Thanks in advance.....

BR,

Chatu
Go to Top of Page
   

- Advertisement -