结果转换
CREATE TABLE [dbo].[DisClass](
[DC001] [nvarchar](50) NULL,
[DC002] [nvarchar](50) NULL,
[DC003] [nvarchar](50) NULL,
[DC004] [float] NULL,
[DC005] [float] NULL,
)
insert into DisClass
select '2012/10','工程二处','筒体组装','14713.55','217.5' union
select '2012/10','工程二处','自动焊接','9735.92','464.5' union
select '2012/10','工程一处','管板加工','5113.675','166' union
select '2012/10','工程一处','装配一组','1747.5','210' union
select '2012/11','工程一处','装配一组','323','2' union
select '2012/11','工程一处','管板加工','444','32' union
select '2012/12','工程一处','管板加工','323.5','134' union
select '2012/12','工程一处','装配一组','1234','33'
--这个只出来DC004列的值
select *
from (select DC003,DC001,DC004 from DisClass) as A
pivot(sum(DC004) for DC001 in ([2012/10],[2012/11],[2012/12])) as B
--这样写不也不能时间表二
select *
from (select DC003,DC001,DC004,DC005 from DisClass) as A
pivot(sum(DC004) for DC001 in ([2012/10],[2012/11],[2012/12])) as B
SELECT dc003 ,sum(CASE WHEN dc001='2012/10' THEN dc004 END)AS [2012/10],
sum(CASE WHEN dc001='2012/10' THEN dc005 END) AS [2012/10],
sum(CASE WHEN dc001='2012/11' THEN dc004 END) AS [2012/11],
sum(CASE WHEN dc001='2012/11' THEN dc005 END) AS [2012/11],
sum(CASE WHEN dc001='2012/12' THEN dc004 END) AS [2012/12],
sum(CASE WHEN dc001='2012/12' THEN dc005 END) AS [2012/12]
FROM disclass
GROUP BY dc003
select dc003,
max(case when dc001='2012/10' then dc004 else null end) dc004,
max(case when dc001='2012/10' then dc005 else null end) dc005,
max(case when dc001='2012/11' then dc004 else null end) dc004,
max(case when dc001='2012/11' then dc005 else null end) dc005,
max(case when dc001='2012/12' then dc004 else null end) dc004,
max(case when dc001='2012/12' then dc005 else null end) dc005
from DisClass
group by dc003
if OBJECT_ID('disclass') is not null
drop table disclass
CREATE TABLE [dbo].[DisClass](
[DC001] [nvarchar](50) NULL,
[DC002] [nvarchar](50) NULL,
[DC003] [nvarchar](50) NULL,
[DC004] [float] NULL,
[DC005] [float] NULL,
)
insert into DisClass
select '2012/10','工程二处','筒体组装','14713.55','217.5' union
select '2012/10','工程二处','自动焊接','9735.92','464.5' union
select '2012/10','工程一处','管板加工','5113.675','166' union
select '2012/10','工程一处','装配一组','1747.5','210' union
select '2012/11','工程一处','装配一组','323','2' union
select '2012/11','工程一处','管板加工','444','32' union
select '2012/12','工程一处','管板加工','323.5','134' union
select '2012/12','工程一处','装配一组','1234','33'
select dc003,SUM(case when dc001='2012/10' then dc004 else null end) as [2012/10-dc004],
SUM(case when dc001='2012/10' then dc005 else null end) as [2012/10-dc005],
SUM(case when dc001='2012/11' then dc004 else null end) as [2012/11-dc004],
SUM(case when dc001='2012/11' then dc005 else null end) as [2012/11-dc005],
SUM(case when dc001='2012/12' then dc004 else null end) as [2012/12-dc004],
SUM(case when dc001='2012/11' then dc005 else null end) as [2012/12-dc005]
from DisClass group by DC003
select a.dc003,
max(dc004_10) dc004_10,
max(dc004_11) dc004_11,
max(dc004_12) dc004_12,
max(dc005_10) dc005_10,
max(dc005_11) dc005_11,
max(dc005_12) dc005_12
from
(select dc003,
[2012/10] as "dc004_10",
[2012/11] as "dc004_11",
[2012/12] as "dc004_12"
from
DisClass
pivot
(
max(dc004)
for dc001 in
([2012/10],[2012/11],[2012/12])
)
as pvt)a
inner join
(select dc003,
[2012/10] as "dc005_10",
[2012/11] as "dc005_11",
[2012/12] as "dc005_12"
from
DisClass
pivot
(
max(dc005)
for dc001 in
([2012/10],[2012/11],[2012/12])
)
as pvt)b
on a.dc003=b.dc003
group by a.dc003