Hi all.
I'm struggling with an SQL query from a particular table. The table is normalised, containing addresses. For the sake of argument, let's call the Table Address.
Address has within it a few fields: AddressID, Line1, Line2, Line3 and AddressType. AddressID is the PK. AddressType is one of several codes, translating to either residential, business, postal, etc.
I need to return a single row that has residential, postal and business address details. I have tried self-joins, but I get 3 rows - each with unique data.
My question is this: How do I get those rows into one. All the data I want is there - but it's separated into 3 rows, and surrounded by NULLs.
I've spent the afternoon searching the web to no avail. (If I see a link to lmgtfy.com in the replies, I'll hunt you down and kill you
) I've tried COALESCE, I've tried CASE - I don't want to do 3 selects into a temp table if I can help it. I've done this before, just can't remember how.
Oh - code is below, in case you want to wade through it.
At the moment it returns this:
Thanks in advance.
I'm struggling with an SQL query from a particular table. The table is normalised, containing addresses. For the sake of argument, let's call the Table Address.
Address has within it a few fields: AddressID, Line1, Line2, Line3 and AddressType. AddressID is the PK. AddressType is one of several codes, translating to either residential, business, postal, etc.
I need to return a single row that has residential, postal and business address details. I have tried self-joins, but I get 3 rows - each with unique data.
My question is this: How do I get those rows into one. All the data I want is there - but it's separated into 3 rows, and surrounded by NULLs.
I've spent the afternoon searching the web to no avail. (If I see a link to lmgtfy.com in the replies, I'll hunt you down and kill you
Oh - code is below, in case you want to wade through it.
Code:
SELECT DISTINCT
CPh.ADDRESS_LINE1 AS 'CurrBusAddr1',
CPh.ADDRESS_LINE2 AS 'CurrBusAddr2',
CPh.ADDRESS_LINE3 AS 'CurrBusAddr3',
CPh.POSTAL AS 'CurrBusAddrCode',
CPh.Suburb AS 'CurrBusAddrSuburb',
PPh.ADDRESS_LINE1 AS 'PrevBusAddr1',
PPh.ADDRESS_LINE2 AS 'PrevBusAddr2',
PPh.ADDRESS_LINE3 AS 'PrevBusAddr3',
PPh.POSTAL AS 'PrevBusAddrCode',
PPh.Suburb AS 'PrevBusAddrSuburb',
CPo.ADDRESS_LINE1 AS 'PostAddr1',
CPo.ADDRESS_LINE2 AS 'PostAddr2',
CPo.ADDRESS_LINE3 AS 'PostAddr3',
CPo.POSTAL AS 'PostAddrCode',
CPo.Suburb AS 'PostAddrSuburb'
FROM dbo.PROSPECT PR
INNER JOIN dbo.PARTNER_ADDR PA ON PA.PARTNER_ID = PR.PARTNER_ID AND PA.CONTACT_PERSON_ID IS NULL
LEFT OUTER JOIN dbo.ADDRESS CPh ON CPh.ADDRESS_ID = PA.ADDRESS_ID AND CPh.ADDR_TYPE_CD = 'Bus'
LEFT OUTER JOIN dbo.ADDRESS PPh ON PPh.ADDRESS_ID = PA.ADDRESS_ID AND PPh.ADDR_TYPE_CD = 'Res'
LEFT OUTER JOIN dbo.ADDRESS CPo ON CPo.ADDRESS_ID = PA.ADDRESS_ID AND CPo.ADDR_TYPE_CD = 'Pos'
WHERE PR.PROSPECT_ID = 120
At the moment it returns this:
Code:
CurrBusAddr1 CurrBusAddr2 CurrBusAddr3 CurrBusCode CurrBusSuburb PrevBusAddr1 PrevBusAddr2 PrevBusAddr3 PrevBusCode PrevBusSuburb PostAddr1 PostAddr2 PostAddr3 PostCode PostSuburb
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL PO BOX 2 NULL 1483 JET PARK
NULL NULL NULL NULL NULL 1 PREV ROAD PREV PARK PREVIOUS 3 2193 GREENSIDE NULL NULL NULL NULL NULL
UNIT A2 JHB 1459 JET PARK NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
Thanks in advance.
Last edited: