2016-05-02 86 views
0

我使用下面的查询,以获得学分,我将如何增加两个条件,如果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   
+0

您正在使用哪些DBMS? –

回答

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    
+0

我的+1是因为“格式良好”的观察。 – sergiol

+0

感谢RBarry Young – jsmabbas