创建数据透视表(sql)时出现奇怪错误

时间:2015-12-17 12:27:08

标签: sql-server select case

我有两张桌子:

表1:MSVTransaction

表2:[MonthlySalary]

我想创建一个数据透视表,比较两个表之间的员工数量:

  select 
      [MSV_EntitledIdNumber],
      [MS_Semel],
      sum(case when [MSV_Month] in (1) then [MSV_PaymentAmount] else 0 end) as JanMSV,
      sum(case when [MS_Month] in (1) then [MS_Amount] else 0 end) as JanSML,
      sum(case when [MSV_Month] in (2) then [MSV_PaymentAmount] else 0 end) as FebMSV,
      sum(case when [MS_Month] in (2) then [MS_Amount] else 0 end) as FebSML,
      sum(case when [MSV_Month] in (3) then [MSV_PaymentAmount] else 0 end) as MarMSV,
      sum(case when [MS_Month] in (3) then [MS_Amount] else 0 end) as MarSML,
      sum(case when [MSV_Month] in (4) then [MSV_PaymentAmount] else 0 end) as AprMSV,
      sum(case when [MS_Month] in (4) then [MS_Amount] else 0 end) as AprSML,
      sum(case when [MSV_Month] in (5) then [MSV_PaymentAmount] else 0 end) as MayMSV,
      sum(case when [MS_Month] in (5) then [MS_Amount] else 0 end) as MaySML,
      sum(case when [MSV_Month] in (6) then [MSV_PaymentAmount] else 0 end) as JunMSV,
      sum(case when [MS_Month] in (6) then [MS_Amount] else 0 end) as JunSML,
      sum(case when [MSV_Month] in (7) then [MSV_PaymentAmount] else 0 end) as JulMSV,
      sum(case when [MS_Month] in (7) then [MS_Amount] else 0 end) as JulSML,
      sum(case when [MSV_Month] in (8) then [MSV_PaymentAmount] else 0 end) as AugMSV,
      sum(case when [MS_Month] in (8) then [MS_Amount] else 0 end) as AugSML,
      sum(case when [MSV_Month] between 1 and 8 then [MSV_PaymentAmount] else 0 
    from [dbo].[MSVTransaction] as msv
    left join [dbo].[MonthlySalary] as SmlTbl on SmlTbl.[MS_InfoEmpNum] =
    msv.MSV_EntitledIdNumber And SmlTbl.MS_Month = msv.MSV_Month
    Where SmlTbl.[MS_MSF_Code] between 4 and 27 and 
    SmlTbl.[MS_Semel] = '666' And msv.[MSV_EntitledIdNumber]= 55555
    and msv.[MSV_MSVT_CodeID] not between 198 and 213
    group by [MSV_EntitledIdNumber],[MS_Semel] 

当我跑步时,我得到:

MSV_EntitledIdNumber    MS_Semel         JanMSV JanSML  FebMSV  FebSML  MarMSV  MarSML  AprMSV  AprSML  MayMSV  MaySML          JunMSV  JunSML  JulMSV  JulSML  AugMSV  AugSML  
    55555             666       2000    2000    5000    5000    6000    6000    8000    8000    7000    7000                       80000    160000  15000   30000   14000   14000   

我想请你注意:

JunMSV  JunSML  JulMSV  JulSML  where the amounts are diffrent between one and other. 

看起来monthSalary的金额增加了一倍(7月为15K * 2,6月为80K * 2) - 为什么?原始数据是:

[MonthlySalary]

MS_InfoEmpNum   MS_Semel    MS_Month    MS_Amount
55555             666           1           2000
55555             666           2           5000
55555             666           3           6000
55555             666           4           8000
55555             666            5          7000
55555             666           6          80000
55555             666           7         15000
55555             666           8          14000

和MSVTransaction:

MSV_EntitledIdNumber    MSV_PaymentAmount   MSV_Month
55555                       2000                1
55555                       5000                2
55555                       6000                3
55555                       8000                4
55555                       7000                5
55555                       75000               6
55555                       5000                6
55555                       4000                7
55555                      11000                7
55555                      14000                8

看起来问题发生在有两条记录的行中,代码只是总和两次但是采取相同的数量。它应该在7月份为20000,在6月份为85K,在这种情况下,msv和sml字段相等。有任何帮助来解决它吗?非常感谢你!

1 个答案:

答案 0 :(得分:1)

MSVTransaction表中有重复的月份。因此,这些值成倍增加。解决此问题的一个好方法是在进行连接之前进行聚合。 from子句看起来像:

from [dbo].[MSVTransaction] msv left join
     (select MSV_EntitledIdNumber, MS_Month, 
             sum(MSV_PaymentAmount) as MSV_PaymentAmount
      from [dbo].[MonthlySalary] SmlTbl
      group by msv.MSV_EntitledIdNumber, SmlTbl.MS_Month
    ) smlTbl
    on SmlTbl.[MS_InfoEmpNum] = msv.MSV_EntitledIdNumber And
       SmlTbl.MS_Month = msv.MSV_Month