你如何得到两个案例陈述的产物

时间:2013-01-22 15:26:32

标签: sql case

我正在尝试使用以下代码组合case语句并继续遇到错误。

SELECT     
        dbo.COL_TBL_WAGES.[Job Group], 
        SUM(CASE
                 WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST' 
                  THEN ([CountOfEMP_TNG_STT_DT] * 
                      (WHEN IsNumeric([TBL_VCOURSE.Length]) 
                        THEN TBL_VCOURSE ELSE 0 END) AS ST_Hours

1 个答案:

答案 0 :(得分:2)

看起来你想要这个,因为你原来有两个CASE表达式:

SELECT  dbo.COL_TBL_WAGES.[Job Group], 
    SUM(CASE 
            WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST' 
            THEN [CountOfEMP_TNG_STT_DT] 
            ELSE 0 END
        * CASE 
            WHEN IsNumeric([TBL_VCOURSE.Length]) = 1 
            THEN TBL_VCOURSE 
            ELSE 0 END
        ) AS ST_Hours

编辑,根据您的评论,您的查询将与此类似:

SELECT dbo.COL_TBL_WAGES.[Job Group], 
    SUM(CASE 
            WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST' 
            THEN [CountOfEMP_TNG_STT_DT] ELSE 0 END 
        * CASE 
            WHEN IsNumeric([COL_TBL_VCOURSE].[Length]) = 1 
            THEN COL_TBL_VCOURSE.[LENGTH]
            ELSE 0 END) AS ST_Hours, 
    SUM(CASE 
            WHEN COL_V_COST.EMP_TNG_RL_CD = 'ST' 
            THEN [CountOfEMP_TNG_STT_DT]*CAST([2011 Total] AS FLOAT) ELSE 0 END 
            * CASE 
                WHEN IsNumeric([COL_TBL_VCOURSE].[LENGTH]) = 1 
                THEN CAST([COL_TBL_VCOURSE].[LENGTH] AS FLOAT) ELSE 0 END) AS ST_Cost