select
DISTINCT CONTACT.CONTACT_ID
, CONTACT.FIRST_NAME
, CONTACT.LAST_NAME
, CONTACT_ADDRESS.COMPANY_NAME
, CONTACT_ADDRESS.ADDRESS_LINE_1
, CONTACT_ADDRESS.ADDRESS_LINE_2
, CONTACT_ADDRESS.CITY
, CONTACT_ADDRESS.STATE_PROVINCE
, CONTACT_ADDRESS.POSTAL_CODE_1
, CONTACT_ADDRESS.POSTAL_CODE_2
, CONTACT_LABEL.CREATED_BY
, CONTACT_NOTE.CONTACT_NOTE_ID
, dbms_lob.substr( CONTACT_NOTE.NOTE_TEXT, 4000, 1) as note_text
, CONTACT_NOTE.NOTE_TYPE
, rep_profile.terr1
, ' ' as Territory
from contact
INNER JOIN CONTACT_ADDRESS ON CONTACT.CONTACT_ID = CONTACT_ADDRESS.CONTACT_ID
INNER JOIN CONTACT_LABEL ON CONTACT.CONTACT_ID = CONTACT_LABEL.CONTACT_ID
INNER JOIN LABEL ON CONTACT_LABEL.LABEL_ID = LABEL.LABEL_ID
LEFT JOIN CONTACT_NOTE ON CONTACT.CONTACT_ID = CONTACT_NOTE.CONTACT_ID
left join
(
rep_profile
inner join terr_user on rep_profile.terr1 = terr_user.terr
inner join terr_version on
(terr_user.version = terr_version.version
and TERR_USER.ROLE_ID ='00007'
and TERR_VERSION.CURRENT_VERSION ='Y'
)
) on contact.contact_id = rep_profile.contact_id
WHERE
LABEL.DESCRIPTION='Holiday Card 2012'
AND ( CONTACT_NOTE.NOTE_TYPE='HC12'
or CONTACT_NOTE.NOTE_TYPE IS NULL
)
I need to put in an if-then-else statement which depends on what the rep_profile.terr1
field shows.
Pseudo-code:
if rep_profile.terr1 = 'W05' then Territory = 'Bob'
else if rep_profile_terr1 = 'W04' then Territory = 'Jack'
...
else Territory = 'N/A'
There are 11 different rep_profile.terr1
. Would appreciate any help on how to best do this. Many thanks in advance!