| Author |
Topic |
|
jubinjose
Starting Member
20 Posts |
Posted - 2007-02-28 : 21:06:23
|
| I have a sql server 2000 table with fields Name, AddressType, AddressAll are varchar fields and name field can be repeated maximum two times(one for business address and other for residence Address)Sample data is as belowName, AddressType, Address---- ----------- -------Jubin, Business, Jubins Business addressJubin, Residence, Jubins Residence AddressBill, Business, Bills business AddressTim, Residence, Tims Residence AddressCan someone help me write a query that outputs in the format Name, Residence Address, Business Address. If no residence address or business addres exists then I need 'NO Address' returned for that column. Expected ResultsJubin, Jubins Residence Address, Jubins Business AddressBill, No Address, Bills Business AddressTim, Tims Residence Address, No Address |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-28 : 21:28:59
|
[code]select [Name] = coalesce(r.Name, b.Name), [Residence Address] = coalesce(r.Address, 'NO Address'), [Business Address] = coalesce(b.Address, 'NO Address')from address r full outer join address bon r.Name = b.Nameand r.AddressType = 'Residence'and b.AddressType = 'Business'[/code] KH |
 |
|
|
jubinjose
Starting Member
20 Posts |
Posted - 2007-03-01 : 00:03:41
|
| Thanks. But that doesn't give expected results. Above query result isjubin, NO Address, jubins residence addressjubin, jubins residence address, jubins business addressbill, NO Address, bills business addresstim, NO Address, tims residence addressbill, bills business address, NO Addresstim, tims residence address, NO Addressjubin, jubins business address, NO Addresswhereas I am trying to getjubin, jubins residence address, jubins business addressbill, NO Address, bills business addresstim, tims residence address, NO Address |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-01 : 00:20:17
|
[code]select [Name] = coalesce(r.Name, b.Name), [Residence Address] = coalesce(r.Address, 'NO Address'), [Business Address] = coalesce(b.Address, 'NO Address')from ( select Name, Address from address where AddressType = 'Residence' ) r full outer join ( select Name, Address from address where AddressType = 'Business' ) bon r.Name = b.Name[/code] KH |
 |
|
|
jubinjose
Starting Member
20 Posts |
Posted - 2007-03-01 : 00:29:33
|
| That works. Thank you very much |
 |
|
|
|
|
|