添加一个从数据透视查询中计算出的列 – 还是从数据透视查询中进行选择?

huangapple go评论70阅读模式
英文:

Adding a calculated column from a pivot query - Or selecting from a pivot query?

问题

我被要求进行有关员工工时的分析。我们公司每周有一定数量的可允许工时(包括加班),这个数量在一年中会有变化。我该如何确定每周的差异以及员工超过可允许工时的次数?

请注意,这实际上是用于调整可允许工时,而不是用于任何纪律措施。如果一周内有多名员工工作时间超过了可允许工时,那么该周的可允许工时可能会增加。

我已经创建了这个查询,但我绝不是一个关于透视查询的专家。

SELECT *
FROM
(SELECT PER_MASTER.EmployeeId, ARC_EARNINGS.EarningsCd, ARC_EARNINGS.CurHoursAmt, VAL_PAYCALENDAR.CheckDate, allowable2.Hours
FROM PER_MASTER 
JOIN ARC_EARNINGS on ARC_EARNINGS.EmployeeId=PER_MASTER.EmployeeId
JOIN VAL_PAYCALENDAR on VAL_PAYCALENDAR.RowId=ARC_EARNINGS.PayCalendarId
JOIN allowable2 on allowable2.Checkdate2=VAL_PAYCALENDAR.CheckDate
WHERE VAL_PAYCALENDAR.CheckDate between '1/5/22' and '1/5/23'
and PER_MASTER.EmployeeID=@Employee
) t
PIVOT(sum(t.CurHoursAmt)
FOR t.EarningsCd in ([Regular],[Overtime])
)p

EarningsCd代表工时的类型(正常、加班、休假等)。
CurHoursAmt代表每种类型的工时数量。
我需要使用表VAL_PAYCALENDAR来获取工资日期。表allowable2是一个简单的表,包含工资日期和总可允许工时数量。不用担心@Employee参数。

我正在SSRS中进行这项工作,同时还有其他信息。这个查询不是可以简单粘贴到Excel中处理的内容。

如果有任何帮助,将不胜感激!

英文:

I have been asked to do an analysis regarding our employee hours. Our company has an allowable number of hours each week (which includes overtime) that changes throughout the year. How would I determine both the difference each week and the number of times an employee has gone over the allowable amount?

Please note, that this is actually being used to readjust the allowable hours and not for any disciplinary measures. If a number of employees worked more than the allowable hours in a particular week, the allowable hours will likely be increased for that week.

I have gone so far as to create this query, but I am by no means an expert on pivot queries.

SELECT *
FROM
(SELECT PER_MASTER.EmployeeId, ARC_EARNINGS.EarningsCd, ARC_EARNINGS.CurHoursAmt, VAL_PAYCALENDAR.CheckDate, allowable2.Hours
FROM PER_MASTER 
JOIN ARC_EARNINGS on ARC_EARNINGS.EmployeeId=PER_MASTER.EmployeeId
JOIN VAL_PAYCALENDAR on VAL_PAYCALENDAR.RowId=ARC_EARNINGS.PayCalendarId
JOIN allowable2 on allowable2.Checkdate2=VAL_PAYCALENDAR.CheckDate
WHERE VAL_PAYCALENDAR.CheckDate between '1/5/22' and '1/5/23'
and PER_MASTER.EmployeeID=@Employee
) t
PIVOT(sum(t.CurHoursAmt)
FOR t.EarningsCd in ([Regular],[Overtime])
)p

EarningsCd is the type of hours (regular, overtime, vacation, etc).
CurHoursAmt is the number of hours for each type.
I need to use the table VAL_PAYCALENDAR for the paycheck date. The table allowable2 is a simple table with the paycheck date and the number of total allowable hours. Don't worry about the @Employee parameter.

I am doing this in SSRS along with other info. This query isn't something I can simply paste into excel and work from there.

Any help with this would be most appreciated!

答案1

得分: 0

计算每周每位员工工作的总小时数(包括常规工时和加班工时)。
将工作的总小时数与每周允许的工时数进行比较。
计算工作的总小时数与每周允许的工时数之间的差异。
统计员工每周超过允许工时数的次数。
以下是一个你可以使用的示例查询:

英文:

Calculate the total number of hours worked each week (including regular and overtime hours) for each employee.
Compare the total number of hours worked to the allowable number of hours for each week.
Calculate the difference between the total number of hours worked and the allowable number of hours for each week.
Count the number of times the employee has gone over the allowable number of hours for each week.
Here is an example query that you could use:

SELECT 
    PER_MASTER.EmployeeId, 
    VAL_PAYCALENDAR.CheckDate,
    SUM(CASE WHEN ARC_EARNINGS.EarningsCd = 'Regular' THEN ARC_EARNINGS.CurHoursAmt ELSE 0 END) AS RegularHours,
    SUM(CASE WHEN ARC_EARNINGS.EarningsCd = 'Overtime' THEN ARC_EARNINGS.CurHoursAmt ELSE 0 END) AS OvertimeHours,
    allowable2.Hours AS AllowableHours,
    SUM(ARC_EARNINGS.CurHoursAmt) AS TotalHours,
    CASE 
        WHEN SUM(ARC_EARNINGS.CurHoursAmt) > allowable2.Hours THEN SUM(ARC_EARNINGS.CurHoursAmt) - allowable2.Hours 
        ELSE 0 
    END AS HoursOverAllowable,
    CASE 
        WHEN SUM(ARC_EARNINGS.CurHoursAmt) > allowable2.Hours THEN 1 
        ELSE 0 
    END AS WeeksOverAllowable
FROM 
    PER_MASTER 
    JOIN ARC_EARNINGS ON ARC_EARNINGS.EmployeeId = PER_MASTER.EmployeeId
    JOIN VAL_PAYCALENDAR ON VAL_PAYCALENDAR.RowId = ARC_EARNINGS.PayCalendarId
    JOIN allowable2 ON allowable2.Checkdate2 = VAL_PAYCALENDAR.CheckDate
WHERE 
    VAL_PAYCALENDAR.CheckDate BETWEEN '1/5/22' AND '1/5/23'
GROUP BY 
    PER_MASTER.EmployeeId, 
    VAL_PAYCALENDAR.CheckDate,
    allowable2.Hours

huangapple
  • 本文由 发表于 2023年4月13日 23:00:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定