如果有人能给我一个提示,我会很高兴。
我有以下查询,其中我根据他为公司工作的时间来计算一个雇员的平均工资。
Declare @data table
(Employee_Number int,
StartDate datetime, EndDate datetime,
Salary bigint)
Insert into @data(Employee_Number, StartDate, EndDate, Salary)
values (4711, '20160201', '20160213', 4000),
(4712, '20160201', '20160309', 8000);
with inc(n) as
(
Select
7 * (ROW_NUMBER() over(order by (select 1)) -1)
From
(Select 1
From (values (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) as x1(n)
Cross Join (values (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) as x2(n)
) x(n)
)
Select
Employee_Number,
Weeks = case when DATEADD(DAY, i.n, StartDate) <= EndDate then DATEADD(DAY, i.n, StartDate) else EndDate end,
Avg = Salary / (CEILING(CAST(DATEDIFF(DAY, StartDate, EndDate) as numeric(9, 1)) / 7) + 1)
From @data d
Inner Join inc i on i.n < ((DATEDIFF(DAY, StartDate, EndDate))) + 7
Order By Employee_Number, Weeks
所以逻辑和我想要的一切都是正确的。但现在我想添加一些东西。使用getdate()
我的意思是今天的实际日期(动态地)。
我想先检查一下getdate() > enddate
。如果是 - >在没有计算平均工资的情况下,将所有工资放在一行。
其次,我想检查一下getdate() < startdate
。如果是 - >使用startdate。如果不是 - &gt;使用getdate()而不是startdate。
如果你能帮我解决这个问题,我会非常感激,因为我不知道。
谢谢你们, 吉克斯
编辑:示例:
上述查询的当前结果:
Employee_Number Weeks Avg
4711 2016-02-01 00:00:00.000 1333.3333333333333333
4711 2016-02-08 00:00:00.000 1333.3333333333333333
4711 2016-02-09 00:00:00.000 1333.3333333333333333
4712 2016-02-01 00:00:00.000 1142.8571428571428571
4712 2016-02-08 00:00:00.000 1142.8571428571428571
4712 2016-02-15 00:00:00.000 1142.8571428571428571
4712 2016-02-22 00:00:00.000 1142.8571428571428571
4712 2016-02-29 00:00:00.000 1142.8571428571428571
4712 2016-03-07 00:00:00.000 1142.8571428571428571
4712 2016-03-09 00:00:00.000 1142.8571428571428571
使用新逻辑:
1) 我想先检查一下getdate()&gt;结束日期。如果是 - >而不是计算平均工资而把所有工资都放在一排。所以如果getdate()是&gt;比使用enddate更新。 结果:
Employee_Number Weeks Avg
4711 2016-02-09 00:00:00.000 4000.0000000000000000
4712 2016-02-01 00:00:00.000 1142.8571428571428571
4712 2016-02-08 00:00:00.000 1142.8571428571428571
4712 2016-02-15 00:00:00.000 1142.8571428571428571
4712 2016-02-22 00:00:00.000 1142.8571428571428571
4712 2016-02-29 00:00:00.000 1142.8571428571428571
4712 2016-03-07 00:00:00.000 1142.8571428571428571
4712 2016-03-09 00:00:00.000 1142.8571428571428571
2)其次,我想检查getdate()&lt;开始日期。如果是 - >使用startdate。如果不是 - &gt;使用getdate()而不是startdate。 结果:
Employee_Number Weeks Avg
4711 2016-02-09 00:00:00.000 4000.0000000000000000
4712 2016-02-11 00:00:00.000 1600.0000000000000000
4712 2016-02-18 00:00:00.000 1600.0000000000000000
4712 2016-02-25 00:00:00.000 1600.0000000000000000
4712 2016-03-03 00:00:00.000 1600.0000000000000000
4712 2016-03-09 00:00:00.000 1600.0000000000000000
答案 0 :(得分:1)
仍然不完全确定你在这里要做什么,但很确定这就是它。这并不是您所说的确切输出,但不确定更改日期的规则是什么。
Declare @data table
(Employee_Number int,
StartDate datetime, EndDate datetime,
Salary bigint)
Insert into @data(Employee_Number, StartDate, EndDate, Salary)
values (4711, '20160201', '20160213', 4000),
(4712, '20160201', '20160309', 8000);
with inc(n) as
(
Select
7 * (ROW_NUMBER() over(order by (select 1)) -1)
From
(Select 1
From (values (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) as x1(n)
Cross Join (values (1), (1), (1), (1), (1), (1), (1), (1), (1), (1)) as x2(n)
) x(n)
)
Select
Employee_Number,
Weeks = case when DATEADD(DAY, i.n, case when getdate() < StartDate then StartDate else getdate() end) <= EndDate then DATEADD(DAY, i.n, case when getdate() < StartDate then StartDate else getdate() end) else EndDate end,
Avg = Salary / (CEILING(CAST(DATEDIFF(DAY, case when getdate() < StartDate then StartDate else getdate() end, EndDate) as numeric(9, 1)) / 7) + 1)
From
@data d
Inner Join inc i on i.n < ((DATEDIFF(DAY, case when getdate() < StartDate then StartDate else getdate() end, EndDate))) + 7
Order By Employee_Number, Weeks