Sql server 将特定员工的列除以另一列

Sql server 将特定员工的列除以另一列,sql-server,select,Sql Server,Select,我有一张这样的桌子: [MS_EmployeeNumber] [MS_Semel] [MS_Month] [MS_Amount] 2222 400 1 20 2222 154 1 40 2222 10 1 14 2222 30

我有一张这样的桌子:

 [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
我想创建一个这样的表(当然没有计算)

我开始写这篇文章:

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 
我非常希望您能帮助我如何根据每位员工的Bruto[MS_Semel=3000]为其创建偏差列,请参见示例。
I如果bruto为null,则将其设置为零(请参见nullif),如果没有[MS_Semel]=3000(对于不存在的员工),则将其设置为“NA”。

是否尝试使用两个查询:一个查询仅包含MS_Semel=3000的行,另一个查询仅返回MS_Semel 3000的行。所以你可以根据MS_EmployeeNumber加入这些数据并进行计算。你明白这个问题了吗?什么是“bruto”?您是否尝试使用两个查询:一个查询仅包含MS_Semel=3000的行,另一个查询仅返回MS_Semel 3000的行。所以你可以根据MS_EmployeeNumber加入这些数据并进行计算。你明白这个问题了吗?什么是“布鲁托”?
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