如果低于某个值,请从日期范围获取最后一个日期记录

时间:2018-02-20 21:05:03

标签: sql date

我查询了哪里有员工名单,以及最近一天工作时间超过30小时的日期。

SELECT T1.Employee, T1.Date, T1.Hours
FROM EmployeeHours T1 
INNER JOIN (SELECT Employee, MAX(Date) AS MAXDATE
            FROM EmployeeHours WHERE Hours < 30 GROUP BY Employee) T2
 ON T1.Employee = T2.Employee AND T1.Date = T2.MAXDATE
 GROUP BY T1.Employee, T1.Date, T1.Hours

我想要的是,如果员工在12周的时间内跌破30小时,我是否可以获得日期最近的日期

我想这里我最大的问题是12周的时间?我想开始日期最好,但我不确定如何使用DATEADD()来实现这一点

1 个答案:

答案 0 :(得分:1)

这是你要找的吗?

    SELECT T1.Employee, T1.Date, T1.Hours
    FROM Employee T1 
    INNER JOIN (SELECT Employee, MAX(Date) AS MAXDATE
                FROM Employee 
                WHERE DATEADD(Day,-12, GETDATE()) <= Date AND DATEADD(Day,0,GETDATE()) > Date
                GROUP BY Employee) T2
     ON T1.Employee = T2.Employee AND T1.Date = T2.MAXDATE
             GROUP BY T1.Employee, T1.Date, T1.Hours