如何计算基于hiredate的度假积累

时间:2013-07-16 13:59:29

标签: sql sql-server

我需要计算每个员工再次接受他们的雇员的总休假时间。获得一名员工很容易

SELECT      PRCo, Employee,  SUM(dbo.PRTH.Hours) AS TotalHrs, 
                         dbo.PREH.HireDate
FROM        A    
WHERE       A.ActiveYN = 'Y') AND (A.EarnCode IN (5249, 5257, 5258, 5279, 5286, 5296, 5309, 7711, 7733)) AND (A. PostDate <= CONVERT(DATETIME, '2013-07-15 00:00:00', 102)) AND (A.PostDate >= CONVERT(DATETIME, '2012-08-21 00:00:00', 102))
GROUP BY A.PRCo, A.Employee, A.PREH.HireDate
HAVING        (A.Employee = 1)

我很难获得所有员工,因为发布日期范围因Hiredate而异。 。我在想使用光标或循环,但我确定它是如何工作的或它将起作用。任何建议都会提前。我用t-sql。感谢

2 个答案:

答案 0 :(得分:0)

您可以使用Employee_Id和Post_Date值创建临时表。在主查询中,使用employee id连接此临时表并获取发布日期值。以下是一个示例查询:

选择
    PRCo,Employee,SUM(dbo.PRTH.Hours)AS TotalHrs,dbo.PREH.HireDate


    一个 加入     #TEMP B. 上     A.Employee = B.Employee_Id WHERE
    A.ActiveYN ='Y')和     (A.EarnCode IN(5249,5257,5258,5279,5286,5296,5309,7711,7733))     AND(A. PostDate&lt; = CONVERT(DATETIME,B.Post_Date,102))     AND(A.PostDate&gt; = CONVERT(DATETIME,B.Post_Date,102)) 通过...分组     A.PRCo,A.Employee,A.PREH.HireDate HAVING
    (A.Employee = 1)

答案 1 :(得分:0)

我希望这段代码会有所帮助:

CREATE TABLE #TEMP(EMPLOYEE_ID INT,POST_DATE DATETIME,HOURS INT) 插入#TEMP VALUES(1,'8/21/2012',DATEDIFF(HH,'8/21/2012',GETDATE())) 插入#TEMP VALUES(2,'5/7/2012',DATEDIFF(HH,'8/21/2012',GETDATE())) SELECT
    PRCo,Employee,SUM(dbo.PRTH.Hours)AS TotalHrs,dbo.PREH.HireDate


    一个 加入     #TEMP B. 上     A.Employee = B.Employee_Id WHERE
    A.ActiveYN ='Y')和     (A.EarnCode IN(5249,5257,5258,5279,5286,5296,5309,7711,7733))     AND(A. PostDate&lt; = CONVERT(DATETIME,B.Post_Date,102))     AND(A.PostDate&gt; = CONVERT(DATETIME,B.Post_Date,102)) 通过...分组     A.PRCo,A.Employee,A.PREH.HireDate HAVING
    (A.Employee = 1)