gpt4 book ai didi

SQL 服务器 : return distinct number of periods

转载 作者:行者123 更新时间:2023-12-04 20:57:52 25 4
gpt4 key购买 nike

我们有以下 SQL Server 2008 示例数据库表,显示每个员工在每个支付周期内的支付信息(每周支付 52 或 53 周,在英国纳税年度之后,因此支付周期的第 1 周是 4 月 6 日及以后)。

我将样本限制为在一系列支付周期内的一名员工,实际数据可以追溯到很多年前。

我需要为每个员工生成从运行查询时起过去 12 周的工资总额。

+------------+------------+---------+-------+
| EMPLOYEEID | PAYELEMENT | AMOUNT | HOURS |
+------------+------------+---------+-------+
| 160062 | 1.0 Basic | 2724.64 | 468 |
+------------+------------+---------+-------+

但是,由于以下原因,我在回溯 12 个不同时期时遇到了一些问题......

  1. 第 1 期有两个条目
  2. 休假时没有月经记录,因此例如缺少第 30、38 和 39 周。在这种情况下,它需要返回直到使用 12 个记录周期。
  3. 年末从第 52 期到第 1 期

我尝试使用每个员工的最后 12 条记录,但上面的项目 1 导致只计算 11 个周期。

我也尝试过在发薪日上使用日期差异,但上面的第 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 |
+------------+------------+------+--------+--------+-------+------+------------------+

最佳答案

你应该试试:

;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]

关于SQL 服务器 : return distinct number of periods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com