我有一张这样的表:
[MS_EmployeeNumber] [MS_Semel] [MS_Month] [MS_Amount]
2222 400 1 20
2222 154 1 40
2222 10 1 14
2222 3000 1 120
3333 400 1 20
3333 154 1 232
3333 3000 1 150
4444 14 1 124
我想创建一个这样的表(没有计算课程)
[MS_EmployeeNumber] [MS_Semel] JanAmount FromBrutoJan FebAmount ...
2222 400 20 0.16=20/120
2222 154 40 0.33=40/120
.
.
3333 400 20 0.13 = 20/150
3333 3000 150 1=150/150
4444 14 124 NA
我开始写这个:
select
ROW_NUMBER() OVER (ORDER BY [MS_EmployeeNumber]) AS Row,
[MS_EmployeeNumber],
[MS_Semel],
sum(case when [MS_Month] in (1) then [MS_Amount] else 0 end) as JanAmount,
sum(case when [MS_Month] in (1) then [MS_Amount] else 0 end) /NULLIF((sum(case when [MS_Month] in (1) And [MS_Semel] = '3000' then [MS_Amount] else 0 end)),0) as SmlFromBruto,
sum(case when [MS_Month] in (2) then [MS_Amount] else 0 end) as FebAmount,
sum(case when [MS_Month] in (3) then [MS_Amount] else 0 end) as MarAmount,
sum(case when [MS_Month] in (4) then [MS_Amount] else 0 end) as AprAmount,
sum(case when [MS_Month] in (5) then [MS_Amount] else 0 end) as MayAmount,
sum(case when [MS_Month] in (6) then [MS_Amount] else 0 end) as JuneAmount,
sum(case when [MS_Month] in (7) then [MS_Amount] else 0 end) as JulAmount,
sum(case when [MS_Month] in (8) then [MS_Amount] else 0 end) as AugAmount
from [dbo].[MonthlySalary] where [MS_MSF_Code] between 4 and 27
group by MS_EmployeeNumber,MS_Semel
order by MS_EmployeeNumber, MS_Semel
我真的希望你能帮助我们为每个员工创建一个偏差列。 [MS_Semel]根据他的 Bruto [MS_Semel = 3000],参见示例。 我bruto为null将其设置为零(请参阅nullif),如果不是[MS_Semel] = 3000,则员工不是exsit将其设置为'NA'。