2015-05-25 80 views
1

我如何實現多案例條件?下面說無效語法多個案例條件

SUM(CASE WHEN b.SnapshotDtm = getdate() THEN   
     CASE WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') THEN 
     (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * 
       (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
     else CASE WHEN (EthnicGroupCd in(2,3,5)) THEN 
       (TechAnnualizedExitsPct_White - TechAnnualizedExitsPct_235) * 
       (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
     ELSE 
      CASE WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') THEN 
        (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * 
         (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
     ELSE CASE WHEN (EthnicGroupCd in(2,3,5)) THEN 
         (TechAnnualizedExitsPct_White - 
          TechAnnualizedExitsPct_235) * 
          (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
      end) as TechExitsDeltaCnt, 
+0

的就是你得到確切的錯誤:'近無效的語法..'? –

+0

附近的')'作爲TechExitsDeltaCnt – VeecoTech

+1

您正在使用哪種RDBMS? – CodeNewbie

回答

3

您的嵌套已關閉。你錯過了幾個END S:

SUM(
    CASE WHEN b.SnapshotDtm = getdate() THEN   
     CASE WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') THEN (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
     ELSE 
      CASE WHEN (EthnicGroupCd in(2,3,5)) THEN (TechAnnualizedExitsPct_White - TechAnnualizedExitsPct_235) * (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
      END 
     END 
    ELSE 
     CASE WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') THEN (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
     ELSE 
      CASE WHEN (EthnicGroupCd in(2,3,5)) THEN (TechAnnualizedExitsPct_White - TechAnnualizedExitsPct_235) * (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
      END 
     END 
    END) as TechExitsDeltaCnt, 
+0

我們並不確定'END's應該在哪裏進入。 –

+0

儘管@RaduGheorghiu,如果你看看錶達式,這看起來很合邏輯。 – Mackan

2

你實際上缺少幾個END S代表每個打開的CASE。試試這個:

SUM(CASE 
     WHEN b.SnapshotDtm = getdate() 
      THEN CASE 
      WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') 
       THEN (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
       ELSE CASE 
        WHEN (EthnicGroupCd in(2,3,5)) 
         THEN (TechAnnualizedExitsPct_White - TechAnnualizedExitsPct_235) * (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
         ELSE CASE 
         WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') 
          THEN (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
          ELSE CASE 
           WHEN (EthnicGroupCd in(2,3,5)) 
            THEN (TechAnnualizedExitsPct_White - TechAnnualizedExitsPct_235) * (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
           END 
          END 
        END 
       END 
    END) AS TechExitsDeltaCnt 
2

else和END標籤錯過

SUM(CASE WHEN b.SnapshotDtm = getdate() 
THEN   
CASE WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') 
    THEN 
     (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * 
    (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) 
     else 
    CASE WHEN (EthnicGroupCd in(2,3,5)) 
     THEN 
     (TechAnnualizedExitsPct_White - TechAnnualizedExitsPct_235) * 
     (TechRepresentCnt_F)/ (365/FiscalYearDayNbr) -------------Else And END TAG is Missing 
ELSE 
     CASE WHEN (GenderCd = 'F' And EthnicGroupCd = 'N/A') 
     THEN 
     (TechAnnualizedExitsPct_M - TechAnnualizedExitsPct_F) * 
     (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) 
     ELSE CASE WHEN (EthnicGroupCd in(2,3,5)) THEN 
     (TechAnnualizedExitsPct_White - 
     TechAnnualizedExitsPct_235) * 
     (TechRepresentCnt_F)/ (365/SnapshotDaysOfYearNbr) -------------Else And END TAG is Missing 
      end) as TechExitsDeltaCnt, 
+1

我認爲這是迄今爲止最好的答案。從這個問題來看,目前還不清楚在哪裏添加「結束」。你選擇了遵循縮進,這有很好的機會正確。此外,你是目前唯一指出缺少'else'子句的人。其他條款不是強制性的,但您通常需要一個條款。 –

+1

我不同意@KlasLindbäck。表達式對這種嵌套沒什麼意義(在外部和內部情況下'GenderCd ='F'和EthnicGroupCd ='N/A'',第二個永遠不會被擊中)。另外,正如你所提到的,'else'既不需要也不需要每次都需要。但無論如何,所有答案都有獨特的解決方案是好的;-) – Mackan

+1

@Mackan好點!您可以通過+1來分析表達式和清晰的縮進! –