多案例条件

时间:2015-05-25 07:23:05

标签: sql

我如何实现多案例条件?下面说的语法无效

 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,

3 个答案:

答案 0 :(得分:3)

你的筑巢已关闭。您错过了一些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)
            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,

答案 1 :(得分:2)

对于每个已打开的END,您实际上错过了一些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 :(得分: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,