Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union Statement Combining unlike queries

Status
Not open for further replies.

sap1958

Technical User
Joined
Oct 22, 2009
Messages
138
Location
US
Here is my code

select coreid,giftamount,addr1,addrcity
from
coretable left outer join gifttable on coreid=giftid
left outer join addrtable on coreid=addrid


select jobsid
from jobstable
left outer join coretable on coreid = jobsid
where jobtype = 'desc'

select schoolid
from schooltable
left outer join coretable on coreid = schoolid
where degree = 'MD'

The only thing common in each query is all must have a coreid.
I also want a case statement that totals the giving such as this
sum(case when giftacct = 'M5' and giftdate >= '7/1/2005 'then giftamount else 0 end)as Giving

Essentially there are three different queries that must be combined into one union query. Any ideas on how to approach this???

Example Output
coreid 01225
addr1 125 State Street
addrcity Williamsburg
degree MD
amount(from case statement) 2500
jobtype desc
 
Try

Code:
select coreid,addr1,addrcity, jobsid, schollid,
sum(case when giftacct = 'M5' then giftamount else 0 end)as Giving

from
coretable left outer join gifttable on coreid=giftid
left outer join addrtable on coreid=addrid
left outer join jobstable on coreid = jobsid and jobtype = 'desc'
left outer join schooltable on coreid = schoolid and degree = 'MD'
where giftdate >= convert(datetime, '07/01/2005', 102)
group by coreid,addr1,addrcity, jobsid, schollid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top