I have rows in a contact details database that look like this:
contactID - bioID - AddressLine1 - City
393 1 1 nowhere st toronto
3921 1 2 somewhere st vancouver
3231 2 1 anywhere rd barrie
1122 2 2 overthere st halifax
I am currently inner joining this to a bio table with columns firstname, lastname etc and the results look like this:
bioid firstname lastname addressline1 city
1 some guy 1 nowhere st toronto
1 some guy 2 somewhere st vancouver
2 that girl 1 anywhere rd barrie
2 that girl 2 overthere st halifax
So I am basically getting 2 rows for every bio. Is there anyway I can select this all as one row like this:
bioid firstname lastname addressline1x1 cityx1 addressline1x2 cityx2
1 some guy 1 nowhere st toronto 2 somewhere st vancouver
2 that girl 1 anywhere rd barrie 2 overthere st halifax
Any help is appreciated.
Thanks,
Thomas
EDIT:
Thanks a lot to Denis and Justin I was able to solve this problem. However, now I have another.
I would like to actually select the addresses as 1 field such as:
bioid firstname lastname primary address secondary address
1 some guy 1 nowhere st, toronto 2 somewhere st, vanvouver
2 that girl 1 anywhere rd, barrie 2 overthere st, halifax
I know I can do it by concatenating columns like:
cd1.addressline1 + ', ' cd1.city AS 'Primary Address'
However, some of the fields are empty in the records, for example if there is no secondary address – so how can I make it so the secondary address doesn’t output ‘, ‘? The fields are empty strings, not NULL values.
Thanks again!
EDIT: I’ve got this working using:
STUFF(COALESCE(', ' + NULLIF(C1.[AddressLine1], ''), '') +
COALESCE(', ' + NULLIF(C1.[AddressLine2], ''), '') +
COALESCE(', ' + NULLIF(C1.[City], ''), '') +
COALESCE(', ' + NULLIF(C1.[State], ''), '') +
COALESCE(', ' + NULLIF(C1.[Country], ''), '') +
COALESCE(', ' + NULLIF(C1.[ZipCode], ''), ''),1, 1, '') AS 'Primary Address'
1 Answer