日期:2014-05-17 浏览次数:20669 次
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'