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)
 Union statement or temp tables

Author  Topic 

omega1983
Starting Member

40 Posts

Posted - 2010-07-07 : 13:47:08
I have to put together two queries that do not have a common value

select giftid,giftamount,coreid,address
from gifts
left outer join core
on giftid=coreid
left outer join address
on giftid=addrid

select jobid,coreid,address
from jobs
left outer join core
on jobsid=coreid
left outer join address
on jobs=addrid


essentially a person can give a gift and not be an employee. Transversely a person can be an employee and not have given anything.
I thought about creating two temp tables but is is better to list both queries and do a union statement

Any best practices ideas that also takes into account optimization? The reason is the code must be saved and used in a crystal report for output. Also a person can only have one coreid so I also want to select a distinct coreid

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-07 : 14:00:07
Could you show us a data example?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

omega1983
Starting Member

40 Posts

Posted - 2010-07-07 : 14:04:06
giftid giftamount coreid jobid address
111 25.00 111 111 21 Marshall Street Providence VA

The giftid,coreid and jobid should be the same however a person can give a gift and not be an employee so we cannot say where giftid=jobid


quote:
Originally posted by tkizer

Could you show us a data example?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-07 : 14:17:54
Please provide a more clear data example. Show us a few rows from each query and then show us how the result set should look like when we combine them. One row of data with matching ids is not enough. We need to see non matching ids and several rows.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -