当前位置: 代码迷 >> Sql Server >> 兑现行列转换
  详细解决方案

兑现行列转换

热度:90   发布时间:2016-04-25 00:45:40.0
实现行列转换
现有代码

if object_id('xmlTest') is not null
DROP TABLE xmlTest
create table xmltest(
id int identity(1,1),
iyear varchar(20),
imonth varchar(20),
iamount varchar(20)
)
insert into xmlTest (iyear,imonth,iamount)
select '2010','Jan','21900' union all
select '2010','Feb','19700' union all
select '2010','Mar','11800' union all
select '2010','Apr','11000' union all
select '2010','May','15000' union all
select '2010','Jun','11800' union all
select '2010','Jul','9800'  union all
select '2010','Aug','21700' union all
select '2010','Sep','11700' union all
select '2010','Oct','11900' union all
select '2010','Nov','0'     union all
select '2010','Dec','0'     union all
select '2011','Jan','27400' union all
select '2011','Feb','29800' union all
select '2011','Mar','25800' union all
select '2011','Apr','26800' union all
select '2011','May','29600' union all
select '2011','Jun','32600' union all
select '2011','Jul','31800' union all
select '2011','Aug','36700' union all
select '2011','Sep','29700' union all
select '2011','Oct','31900' union all
select '2011','Nov','34800' union all
select '2011','Dec','24800' union all
select '2012','Jan','10000' union all
select '2012','Feb','11500' union all
select '2012','Mar','12500' union all
select '2012','Apr','15000' union all
select '2012','May','11000' union all
select '2012','Jun','9800'  union all
select '2012','Jul','11800' union all
select '2012','Aug','19700' union all
select '2012','Sep','21700' union all
select '2012','Oct','21900' union all
select '2012','Nov','22900' union all
select '2012','Dec','20800'

想得到
      JAN  FEB MAR APR MAY JUN JUL AU SEP OCT NOV DEC
2010
2011
2012

这样的结果应该怎么写SQL?
------最佳解决方案--------------------
--IF OBJECT_ID('xmlTest') IS NOT NULL 
--    DROP TABLE xmlTest
--CREATE TABLE xmltest
--    (
--      id INT IDENTITY(1, 1) ,
--      iyear VARCHAR(20) ,
--      imonth VARCHAR(20) ,
--      iamount VARCHAR(20)
--    )
--INSERT  INTO xmlTest
--        ( iyear ,
--          imonth ,
--          iamount
--        )
--        SELECT  '2010' ,
--                'Jan' ,
--                '21900'
--        UNION ALL
  相关解决方案