多案例条件

时间:2016-05-02 13:27:37

标签: sql

我使用以下查询获取信用点,如果ID_Points = 2然后0.5和ID_points = 3然后0.25

,我将如何添加另外两个条件
SELECT     
    Attend_ID, 
    Attend_Date, 
    ID_Points, 
    Employee_ID, 
    First_Name, 
    Last_Name, 
    NextDate, 
    NEXT123, 
    Difference, 
    DAY90CREDIT, 
    CREDITDATE, 
    CASE 
        WHEN (day90Credit = 0 AND CreditDate < Getdate()) 
          OR DateAdd(DAY, 90, attend_date) < COALESCE (NextDate, GETDATE()) 
         AND ID_Points = 1 THEN 1 
        ELSE 0 END AS TOTALCREDIT                  
FROM
     dbo.Test_DiffNintyDays           

1 个答案:

答案 0 :(得分:3)

良好的格式化使这更容易,更明显:

假设您的描述是文字:

 SELECT     Attend_ID, 
            Attend_Date, 
            ID_Points, 
            Employee_ID, 
            First_Name, 
            Last_Name, 
            NextDate, 
            NEXT123, 
            Difference, 
            DAY90CREDIT, 
            CREDITDATE, 
            CASE 
                WHEN (day90Credit = 0 AND CreditDate < Getdate()) 
                  OR DateAdd(DAY, 90, attend_date) < COALESCE (NextDate, GETDATE()) 
                 AND ID_Points = 1 THEN 1
                WHEN ID_Points = 2 THEN 0.5 
                WHEN ID_Points = 3 THEN 0.25
                ELSE 0 END AS TOTALCREDIT                  
  FROM     dbo.Test_DiffNintyDays             

然而,我怀疑你真的是这个意思:

 SELECT     Attend_ID, 
            Attend_Date, 
            ID_Points, 
            Employee_ID, 
            First_Name, 
            Last_Name, 
            NextDate, 
            NEXT123, 
            Difference, 
            DAY90CREDIT, 
            CREDITDATE, 
            CASE 
                WHEN (day90Credit = 0 AND CreditDate < Getdate()) 
                  OR DateAdd(DAY, 90, attend_date) < COALESCE (NextDate, GETDATE()) 
                 AND ID_Points = 1 THEN 1
                WHEN (day90Credit = 0 AND CreditDate < Getdate()) 
                  OR DateAdd(DAY, 90, attend_date) < COALESCE (NextDate, GETDATE()) 
                 AND ID_Points = 2 THEN 0.5 
                WHEN (day90Credit = 0 AND CreditDate < Getdate()) 
                  OR DateAdd(DAY, 90, attend_date) < COALESCE (NextDate, GETDATE()) 
                 AND ID_Points = 3 THEN 0.25
                ELSE 0 END AS TOTALCREDIT                  
  FROM     dbo.Test_DiffNintyDays