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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Joining multiple Queries on fly

Author  Topic 

calibar2k
Starting Member

2 Posts

Posted - 2010-01-08 : 18:37:43
Query_1 : id,service,year,revenue
Query_2 : id,service,year,revenue
Query_3 : id,service,year,revenue

Three Queries are dynamic queries, id is common in all three queries but service value will change. I need to join three queries and has to take the sum of three revenues.

I have a problem while any of the Query returning zero and the rest have rows, my join condition is failing and zero rows are returning in the final since iam doing inner join. can any one give me the solution...

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-08 : 18:46:28
You would need to use LEFT JOIN and handle the NULL with a COALESCE or ISNULL() function.

But first...show us the query.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-09 : 10:33:57
quote:
Originally posted by calibar2k

Query_1 : id,service,year,revenue
Query_2 : id,service,year,revenue
Query_3 : id,service,year,revenue

Three Queries are dynamic queries, id is common in all three queries but service value will change. I need to join three queries and has to take the sum of three revenues.

I have a problem while any of the Query returning zero and the rest have rows, my join condition is failing and zero rows are returning in the final since iam doing inner join. can any one give me the solution...


I think what you need is to put partial query results to temp tables and take UNION ALL among them and take sum after applying group by on relevant fields.
Go to Top of Page

calibar2k
Starting Member

2 Posts

Posted - 2010-01-12 : 10:38:28
Thank you Visakh, your logic works for me.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-12 : 10:46:14
welcome
Go to Top of Page
   

- Advertisement -