t-SQL-在现有代码内确定员工的工作天数(以计算每个期间的员工FTE数量)

时间:2018-11-14 00:41:07

标签: sql-server tsql ssms

我的目标是计算每个工作时期的全职等效人员(FTE)的数量-全职和兼职-

我有以下代码来计算按地区和状态(FT = 1 / PT = 2)分组的FT / PT员工人数:

代码1(主要):

DECLARE @MonthBOP date = '07-01-2017'
DECLARE @MonthEOP date = '09-30-2018'
DECLARE @GLRegionsWHID int = 705
DECLARE @GLProgramsWHID int = -1

SELECT 
   glrn.WHID AS [RegionID]
  ,dol.WHID  AS [StatusType]
  ,COUNT (*) AS [CountClients]

FROM [WH].[Dimension].[PaycomEmployee] empl
INNER JOIN Dimension.PaycomDOLStatus dol ON empl.PaycomDOLStatusFK = dol.WHID
INNER JOIN Dimension.PaycomDepartment dept ON empl.PaycomDepartmentFK = dept.WHID
INNER JOIN Dimension.GLProgram glpgm ON dept.GLProgramFK = glpgm.WHID
INNER JOIN Dimension.GLRegion glrn ON glpgm.GLRegionFK = glrn.WHID

WHERE 

((dol.WHID IN (1,2)))
AND ((glrn.WHID IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
AND ((glpgm.WHID IN (@GLProgramsWHID)) OR (-1 IN (@GLProgramsWHID)))

GROUP BY
    glrn.WHID
   ,dol.WHID

结果如下(如下):

enter image description here

但是我还有另一个代码部分,它确定-员工的工作时间:

代码2:

Min(@MonthEOP, empl.DateTerminated) - Max(@MonthBOP, empl.DateHired) + 1 =
 [Number Days employed in Period]
WHERE empl.DateTerminated > @MonthBOP 

我的问题是-如何正确地组合主代码(代码1)和附加部分(代码2)-因此将全部合并为一个代码 或-代码1中对代码2的任何引用

我的目标是确定员工的工作时间并将其纳入 代码1(主要)

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您不能像在代码2中那样使用MIN()MAX()

改为使用CASE

CASE
    WHEN @MonthBOP > empl.DateTerminated THEN @MonthBOP
    WHEN @MonthEOP < empl.DateTerminated THEN @MonthEOP
    ELSE empl.DateTerminated
END
-
CASE
    WHEN @MonthEOP < empl.DateHired THEN @MonthEOP
    WHEN @MonthBOP > empl.DateHired THEN @MonthBOP
    ELSE empl.DateHired
END
    AS [Number Days employed in Period]