我们有以下SQL Server 2008示例数据库表,显示每个员工在每个支付期间的工资信息(每周工资为52或53周,在英国纳税年度之后,因此第1周的工资期为4月6日及之后)。
我将样本限制在一个支付期间的一名员工,实际数据可以追溯到很多年。
我需要在运行查询后的最后12周工资中为每位员工生成总计。
+------------+------------+---------+-------+
| EMPLOYEEID | PAYELEMENT | AMOUNT | HOURS |
+------------+------------+---------+-------+
| 160062 | 1.0 Basic | 2724.64 | 468 |
+------------+------------+---------+-------+
然而,由于以下原因,我有一些问题可以追溯到12个不同时期......
我尝试使用每位员工的最后12条记录,但上面的第1项只计算了11个期间。
我也尝试在paydate上使用日期差异,但上面的第2项导致时间段丢失。
我是否需要为每位员工添加一个索引以显示12个单独的句点?
+------------+------------+------+--------+--------+-------+------+------------------+
| EMPLOYEEID | PAYELEMENT | YEAR | PERIOD | AMOUNT | HOURS | RATE | PAYDATE |
+------------+------------+------+--------+--------+-------+------+------------------+
| 160062 | 1.0 Basic | 2017 | 29 | 311.22 | 39 | 7.98 | 20/10/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 31 | 311.22 | 39 | 7.98 | 03/11/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 32 | 311.22 | 39 | 7.98 | 10/11/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 33 | 311.22 | 39 | 7.98 | 17/11/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 34 | 311.22 | 39 | 7.98 | 24/11/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 35 | 311.22 | 39 | 7.98 | 01/12/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 36 | 183.54 | 23 | 7.98 | 08/12/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 37 | 311.22 | 39 | 7.98 | 15/12/2016 00:00 |
| 160062 | 1.0 Basic | 2017 | 40 | 311.22 | 39 | 7.98 | 05/01/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 41 | 311.22 | 39 | 7.98 | 12/01/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 42 | 311.22 | 39 | 7.98 | 19/01/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 43 | 311.22 | 39 | 7.98 | 26/01/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 44 | 311.22 | 39 | 7.98 | 02/02/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 45 | 311.22 | 39 | 7.98 | 09/02/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 46 | 311.22 | 39 | 7.98 | 16/02/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 47 | 311.22 | 39 | 7.98 | 23/02/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 48 | 127.68 | 16 | 7.98 | 02/03/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 49 | 311.22 | 39 | 7.98 | 09/03/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 50 | 247.38 | 31 | 7.98 | 16/03/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 51 | 311.22 | 39 | 7.98 | 23/03/2017 00:00 |
| 160062 | 1.0 Basic | 2017 | 52 | 311.22 | 39 | 7.98 | 30/03/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 1 | 247.38 | 31 | 7.98 | 06/04/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 1 | 0 | 0 | 7.75 | 06/04/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 2 | 311.22 | 39 | 7.98 | 13/04/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 3 | 255.36 | 32 | 7.98 | 20/04/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 4 | 247.38 | 31 | 7.98 | 27/04/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 5 | 311.22 | 39 | 7.98 | 04/05/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 6 | 127.68 | 16 | 7.98 | 11/05/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 7 | 247.38 | 31 | 7.98 | 18/05/2017 00:00 |
| 160062 | 1.0 Basic | 2018 | 8 | 277.31 | 34.75 | 7.98 | 25/05/2017 00:00 |
+------------+------------+------+--------+--------+-------+------+------------------+
答案 0 :(得分:1)
你应该尝试:
;WITH Top12Periods AS (
SELECT TOP 12 [YEAR], [PERIOD]
FROM @employeeTable
GROUP BY [YEAR], [PERIOD]
ORDER BY [YEAR] DESC, [PERIOD] DESC
)
SELECT [EMPLOYEEID], [PAYELEMENT], SUM([AMOUNT]) AS TOTAL_AMOUNT, SUM([HOURS]) AS TOTAL_HOURS, AVG([RATE]) AS AVERAGE_RATE, MIN ([PAYDATE]) [MIN_PAYDATE]
FROM @employeeTable et
JOIN Top12Periods p ON et.[YEAR] = p.[YEAR] AND et.[PERIOD] = p.[PERIOD]
GROUP BY [EMPLOYEEID], [PAYELEMENT]
答案 1 :(得分:0)
这是另一种方法:
SELECT
EmployeeID,
PayElement,
sum(Amount) as Amount,
sum(Hours) as Hours
FROM
(
SELECT *
,dense_rank() OVER (PARTITION BY EmployeeId ORDER BY PayDate DESC) as rank
FROM
Pay
) ranked
WHERE
rank <= 12
GROUP BY
EmployeeID,
PayElement
如果零值导致问题,则只需将WHERE Amount > 0
(或者<> 0
)添加到内部查询中。根据您的数据,您可能还需要将PayElement
添加到Partition By
子句(或内部WHERE
子句)。