SQL Server中的数据透视表问题

时间:2011-07-30 04:55:24

标签: sql sql-server tsql pivot

create table newtest (
  Section varchar(50),
  Department varchar(50), 
  salesamount float
)

Insert into newtest
select 'Sec1','IT',2000.89 union
select 'Sec1','IT',1000.89 union
select 'Sec1','IT',3000.89 union
select 'Sec1','BPO',2000.89 union
select 'Sec2','BPO',5000.89 union
select 'Sec2','IT',2700.89 union
select 'Sec2','BPO',2000.89 union
select 'Sec3','IT',6000.89 union
select 'Sec3','IT',2000.89 union
select 'Sec3','BPO',9000.89 union
select 'Sec4','IT',2000.89 union
select 'Sec4','BPO',6000.89 union
select 'Sec3','BPO',1000.89 union
select 'Sec4','IT',3000.89

select * from newtest

  Select section, department, SUM(salesamount) 
    from newtest 
Group by Section, Department

- 使用数据透视

select [Sec1] AS Sec1BPO,
       [Sec2] AS Sec2BPO,
       [Sec3] AS Sec3BPO,
       [Sec4] AS Sec4BPO,
       [Sec1] AS Sec1IT,
       [Sec2] AS Sec2IT,
       [Sec3] AS Sec3IT,
       [Sec4] AS Sec4IT
  from (select Section, Department, salesamount 
          from newtest) as Sourcetable
 PIVOT( SUM([salesamount])
      FOR [Section] IN ([Sec1],[Sec2],[Sec3],[Sec4]) ) AS Pivottable
ORDER BY Department

请帮我解决这个问题....我在执行上面的代码时得到了结果

Sec1BPO  Sec2BPO  Sec3BPO   Sec4BPO  Sec1IT   Sec2IT   Sec3IT    Sec4IT
-------------------------------------------------------------------------
2000.89  7001.78  10001.78  6000.89  2000.89  7001.78  10001.78  6000.89
6002.67  2700.89  8001.78   5001.78  6002.67  2700.89  8001.78   5001.78

但我需要

Sec1BPO  Sec2BPO  Sec3BPO   Sec4BPO  Sec1IT   Sec2IT   Sec3IT   Sec4IT
-----------------------------------------------------------------------
2000.89  7001.78  10001.78  6000.89  6002.67  2700.89  8001.78  5001.78

请帮助我获得理想的结果。

2 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

select Sec1BPO,
       Sec2BPO,
       Sec3BPO,
       Sec4BPO,
       Sec1IT,
       Sec2IT,
       Sec3IT,
       Sec4IT
  from (select Section + Department AS SectDept, salesamount 
          from newtest) as Sourcetable
 PIVOT( SUM([salesamount])
      FOR SectDept IN (Sec1BPO, Sec2BPO, Sec3BPO, Sec4BPO,
                       Sec1IT,  Sec2IT,  Sec3IT,  Sec4IT) ) AS Pivottable
ORDER BY 1

<强>更新

只需提醒一句 - 上面的解决方案,一般情况下应用,可能会使您将可能无法完成的数据汇总在一起的风险。也就是说,以您的当前示例为例,如果它包含Sec1BPOSec1BPO的行,则Section和{{1}的组合},这两种组合最终会以Department连接起来,从而加起来,这显然是错误的。

因此,在连接部分时可能应该使用某些分隔符来排除这些重复项,例如空格或已知肯定不存在于要连接的值中的其他字符。我的意思是这样的:

Sec1BPO

(感谢@Conrad Frix提出这个问题。)

答案 1 :(得分:1)

;WITH CTE AS (
select 
       Department,
       [Sec1]  ,
       [Sec2] ,
       [Sec3] ,
       [Sec4] 
  from (select Section, Department, salesamount 
          from @newtest) as Sourcetable
 PIVOT( SUM([salesamount])
      FOR [Section] IN ([Sec1],[Sec2],[Sec3],[Sec4]) ) AS Pivottable
)
SELECT 
    a.sec1 Sec1bpo, a.sec2 sec2bpo, a.sec3 sec3bpo, a.sec4 sec4bpo,
    b.sec1 Sec1it, b.sec2 sec2it, b.sec3 sec3it, b.sec4 sec4it
FROM 
    cte a, cte b
WHERE
   a.department = 'BPO' and B.department = 'it'​

要查看它的工作,请转到Data.SE查询SO Question 6881168

注意我使用了Table变量而不是表,因为Create table不适用于Data.SE

输出

Sec1bpo sec2bpo sec3bpo  sec4bpo Sec1it  sec2it  sec3it  sec4it  
------- ------- -------- ------- ------- ------- ------- ------- 
2000.89 7001.78 10001.78 6000.89 6002.67 2700.89 8001.78 5001.78