select语句中的多个条件

时间:2017-03-13 03:49:26

标签: sql sql-server

我有一个包含多个条件的SELECT语句。

以下是我的查询代码:

SELECT 
    G.CLOSED_MONTH, EE.BASIC_SALARY, V.EMPLOYEE_NO, G.CLOSED_YEAR
    CASE 
       WHEN ((G.CLOSED_YEAR = YEAR (GETDATE()) AND G.CLOSED_MONTH <= MONTH (GETDATE()) 
          THEN EE.BASIC_SALARY) 
       WHEN (G.CLOSED_YEAR BETWEEN EE.DATE_JOINED AND YEAR (GETDATE()) 
          THEN CASE 
                  WHEN V.ACTIVITY_TYPE = '0'
                     THEN EE.BASIC_SALARY - V.ACTIVITY_VALUE 
                     ELSE EE.BASIC_SALARY + V.ACTIVITY_VALUE 
               END)
       ELSE ((G.CLOSED_YEAR = YEAR (EE.DATE_JOINED) AND G.CLOSED_MONTH >= MONTH (EE.DATE_JOINED)
        THEN CASE WHEN V.ACTIVITY_TYPE = '0'
        THEN EE.BASIC_SALARY - V.ACTIVITY_VALUE 
    ELSE 
        EE.BASIC_SALARY + V.ACTIVITY_VALUE
    END) 
    END AS NEW_BASICSALARY
FROM 
    GENERAL_SPECIFICATION G
JOIN 
    EMPLOYEE_EMPLOYMENT EE ON G.COMPANY_CODE = EE.COMPANY_CODE
JOIN 
    V_EMPLOYEE_PAYACT V ON EE.EMPLOYEE_ID = V.EMPLOYEE_ID

当我尝试运行查询时,它一直收到错误:

  

Msg 156,Level 15,State 1,Line 2
  关键字&#39; CASE&#39;附近的语法不正确。

1 个答案:

答案 0 :(得分:2)

你在第一个CASE

之前缺少','