if then语句在select查询中

时间:2012-10-23 19:09:10

标签: sql select if-statement

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
        )

我需要输入if-then-else语句,该语句取决于rep_profile.terr1字段显示的内容。

的伪代码:

if rep_profile.terr1 = 'W05' then Territory = 'Bob'
else if rep_profile_terr1 = 'W04' then Territory = 'Jack'
...
else Territory = 'N/A'

有11种不同的rep_profile.terr1。非常感谢有关如何做到这一点的任何帮助。非常感谢提前!

1 个答案:

答案 0 :(得分:4)

case rep_profile.terr1 
    when 'W04' then 'Jack' 
    when 'W05' then 'Bob' 
    else 'N/A'
end as Territory