2017-03-13 87 views
0

我需要在where子句中包含一個IF或CASE語句,這將爲我提供一個依賴於db中數據的記錄。在where子句中包含IF

我想要做的是選擇在relationship_code_id中具有特定代碼的某個聯繫人,以指示他們已被分配POA(代碼爲45)。如果聯繫人沒有POA,則顯示主要聯繫人(main_contact = 1)。但是,任何具有POA的聯繫人都優先於主要聯繫人。所以如果Joe Bloggs被分配了主要聯繫人,但Anne Nickols被分配了POA,這個節目Anne不是Joe。

(包括在TNTGROUP表此信息)

反正我能做到這一點?

道歉,如果這沒有意義。

這是我下面的代碼:

SELECT 

    TNTTITLE.Description_VC AS TITLE 
    ,TNTCONTACT.Forename_VC AS FORENAME 
    ,TNTCONTACT.Surname_VC AS SURNAME 
    ,TNTCONTACTEMAIL.Reference_VC AS EMAIL 
    ,TNTHOMENUMBER_GRP.Reference_VC AS GROUP_HOME_NUMBER 
    ,RENTACC.ID AS RENT_ACCOUNT_ID 
    ,CONVERT(DATE,RENTACC.AccountStartDate) AS RENT_ACCOUNT_START_DT 
    ,CASE 
     WHEN LETOFFERS.Current_Status_ID = 42 THEN 'Complete' 
    ELSE 'OTHER' 
    END 
     AS STATUS 
    ,ASSETTYPES.Description_VC AS ASSET_NAME 
    ,CASE WHEN (SELECT MainAsset FROM RENTACC HAVING COUNT(*) > 1) THEN 1 ELSE 0 END 


    FROM RENT_ACCOUNTS AS RENTACC 

    INNER JOIN LETTING_OFFERS_T AS LETOFFERS 
     ON RENTACC.ID = LETOFFERS.Rent_Account_Reference_VC 
      AND RENTACC.AccountEndDate IS NULL 
      AND RENTACC.AccountType = 'T' 

    LEFT JOIN Letting_SYS_Master_T AS LETCODES 
     ON LETOFFERS.Current_Status_ID = LETCODES.CODE_ID 
      AND LETCODES.Type_ID = '14' 

    LEFT JOIN Asset_Assets_T AS ASSETS 
     ON RENTACC.MainAsset = ASSETS.Asset_ID 

    LEFT JOIN Asset_Types_T AS ASSETTYPES 
     ON ASSETS.Asset_TYPE_ID = ASSETTYPES.Asset_Type_ID 


    /**** MAIN CONTACT INFORMATION ****/ 

    LEFT JOIN Shared_Addresses_T AS TNTADDRESS 
     ON ASSETS.Address_ID = TNTADDRESS.Address_ID 

    LEFT JOIN Contact_Group_Contacts_T AS TNTGROUP 
     ON RENTACC.ContactDatabaseReference = TNTGROUP.GROUP_ID 
      AND TNTGROUP.Main_Group_Contact_BT = 1 
      AND TNTGROUP.Removed_BT = 0 

    LEFT JOIN CONTACT_CONTACTS_T AS TNTCONTACT 
     ON TNTGROUP.Contact_ID = TNTCONTACT.Contact_ID 
      AND TNTCONTACT.Current_Status_ID = 65 
      AND TNTCONTACT.Deceased_Date_DT IS NULL 

    LEFT JOIN Shared_Codes_T AS TNTTITLE 
     ON TNTCONTACT.Title_Code_ID = TNTTITLE.Code_ID 
      AND TNTTITLE.Code_ID IN (103,104,105,106) 

    /**** CONTACT EMAIL INFORMATION ****/ 

    LEFT JOIN Contact_Contact_Details_T AS TNTCONTACTEMAIL 
     ON TNTCONTACT.Contact_ID = TNTCONTACTEMAIL.Contact_ID 
      AND TNTCONTACTEMAIL.SYS_Number_Code_ID = 75 
      AND TNTCONTACTEMAIL.Expiry_Date_DT IS NULL 


    /**** GROUP HOME NUMBER INFORMATION ****/ 

    LEFT JOIN Contact_Group_Details_T AS TNTHOMENUMBER_GRP 
     ON RENTACC.ContactDatabaseReference = TNTHOMENUMBER_GRP.Group_ID 
      AND TNTHOMENUMBER_GRP.Expiry_Date_DT IS NULL 
      AND TNTHOMENUMBER_GRP.SYS_Number_Code_ID = 71 
+0

它通常是更好地使用和/或替代情況/如果WHERE和ON子句。 – jarlh

+0

可以請您最小化您的代碼,只向我們展示您需要幫助的內容。真的很難閱讀和理解101加入 – Stephen

+0

@jarlh,甚至最好不要使用他們中的任何一個作爲我的回答:D – Hogan

回答

0

我會做這樣的 - 我會離開加入到POA聯繫信息的一部分加入檢查碼ID爲45 - 因此它會如果代碼ID不是45,則爲空。然後我只用COALESCE來提取所需的值。你不給了很多有關你的數據模型,但它應該是這樣的:

沒有IFCASE需要 - 這一切都是建立在過濾器上加入的所有SQL平臺的優化評估。

SELECT 
    -- blah blah blah.. current select 
    -- TNTGROUP won't be null if relationship code is 45 
    COALESCE(TNTGROUP.CONTACT_INFO,a_table.main_contact) as Contact 
FROM a_table 
LEFT JOIN TNTGROUP ON 
    -- How TNTGROUP joins to a_table (which you don't say) 
    a_table.id = TNTGROUP.id 
    -- Only if valid 
    AND TNTGROUP.relationship_code_id = 45