【问题标题】:Get the reduced or increased working hours获得减少或增加的工作时间
【发布时间】:2022-01-14 10:55:49
【问题描述】:

我需要从员工列表中获取过去 12 个月与当前月份相比减少或增加的工作时间、首发人员和离职人员。

我遇到的问题是我没有得到上个月的离职人员,但本月的工作时间为空,即所谓的离职人员。

这是我尝试过的:

WITH CURRENTbase AS
(
    SELECT Employee, Work_hrs,monthdate
    FROM MyTable
    WHERE monthlydate >= '2020-mar-01'
),
PREVIOUSbase AS
(
    SELECT Employee, Work_hrs,monthdate
    DATEADD(MONTH,1,monthdate) monthdate
    FROM MyTable
    WHERE  monthdate >= '2020-mar-01'
)
SELECT 
    B.*, A.*,
    A.monthdate,
    ISNULL(B.Work_hrs,0) PreviousHRS,
    ISNULL(A.Work_hrs,0) CurrentHRS,
    CASE 
        WHEN b.Work_hrs is null THEN 'Starter'
        WHEN a.Work_hrs is null THEN 'Leaver'
        WHEN a.Work_hrs > b.Work_hrs THEN 'hrs_Increase'
        WHEN b.Work_hrs > a.Work_hrs THEN 'hrs_Decrease'
        WHEN b.Work_hrs = a.Work_hrs THEN 'NoChange'
        ELSE 'Check'
    END Status
FROM CURRENTbase A

LEFT JOIN PREVIOUSbase B 
    ON A.Employee = B.Employee AND A.monthdate = B.monthdate

WHERE a.CensusDate >= '2020-apr-01'

GROUP BY
    A.monthdate,
    ISNULL(B.Work_hrs,0) PreviousHRS,
    ISNULL(A.Work_hrs,0) CurrentHRS,
    CASE 
        WHEN b.Work_hrs is null THEN 'Starter'
        WHEN a.Work_hrs is null THEN 'Leaver'
        WHEN a.Work_hrs > b.Work_hrs THEN 'hrs_Increase'
        WHEN b.Work_hrs > a.Work_hrs THEN 'hrs_Decrease'
        WHEN b.Work_hrs = a.Work_hrs THEN 'NoCHange'
        ELSE 'check'
    END

【问题讨论】:

  • 另外,请告诉我们您如何知道 EMP1 是否已离开或本月只有零小时?
  • 嗨 Filburt,我想主要查看以前和当前的工作时间值,然后我可以自己为状态创建一个案例声明。非常感谢。

标签: sql sql-server join


【解决方案1】:

如果您使用 MS SQL SERVER 2012 或更高版本,您可以尝试此查询,采用order by 以满足您的需求

SELECT *
  ,CASE 
      WHEN LEAD(WORKING_HOURS) OVER (
              PARTITION BY Employee ORDER BY Year DESC
                  ,Month DESC
              ) IS NULL
          THEN 'Starter'
      WHEN LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year ASC
                ,Month ASC
            ) IS NULL
        THEN 'Leaver'
      WHEN LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year ASC
                ,Month ASC
            ) > LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year DESC
                ,Month DESC
            )
        THEN 'hrs_Increase'
      WHEN LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year DESC
                ,Month DESC
            ) > LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year ASC
                ,Month ASC
            )
        THEN 'hrs_Decrease'
      WHEN LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year DESC
                ,Month DESC
            ) = LEAD(WORKING_HOURS) OVER (
            PARTITION BY Employee ORDER BY Year ASC
                ,Month ASC
            )
        THEN 'NoCHange'
      ELSE 'check'
      END
 FROM (
     SELECT Employee
        ,MONTH(monthdate) Month
        ,YEAR(monthdate) Year
        ,SUM(Work_hrs) AS WORKING_HOURS
    FROM MyTable
    GROUP BY Employee
       ,MONTH(monthdate)
       ,YEAR(monthdate)
  ) a

【讨论】:

  • Dourayd 感谢您的帮助,但如果我没有正确解释,这不是我要问的。我遇到的问题是那些客户离开的价值是 0 并且没有显示在下个月的价值中,所以我不能将他们分配给离开者。
  • 如果我理解你的意思,你在识别离职人员时遇到了问题。如果下个月没有数据,那么通常铅会给你NULL。如果不能,请您进一步解释或根据您提供所需输出的数据给我们。
猜你喜欢
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多