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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

union statement for different types of data 1

Status
Not open for further replies.

sap1958

Technical User
Joined
Oct 22, 2009
Messages
138
Location
US
I am trying to combine relationship with giving data. Here is a portion of my code

select distinct coreid,name,address,birthday,relation
from core_table left outer join address_table
on addrid=coreid
left outer join relation
on relid=coreid
group by coreid,name,address,birthday,relation

union
select distinct coreid,name,address,birthday,relation,
sum(case when gifttype ='g' then amount else 0 end) as amount
from core_table left outer join address_table
on addrid=coreid
left outer join gifts
on giftid=coreid
left outer join relation
on relid=coreid
group by coreid,name,address,birthday,relation,gifttype

Any thing wrong with this? essentially a person qualifies based on relationship or giving. Therefore I use a union statement. I keep getting group errors
 
Your second query includes gifttype in the group by, but... you are not *really* selecting it. gifttype only appears in an aggregate (the sum). Remove gifttype from the group by.

Your next problem is likely to be that the two queries do not return the same number of columns.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top