cuiyi's blog(崔毅 crazycy)

          記錄點滴 鑒往事之得失 以資于發展
          數據加載中……

          SQLSERVER: ROWS TO COLUMNS 行轉列 (二)

          Just done by me based on the previous article: SQLServer: Rows to columns

          Use table 
          CREATE TABLE [dbo].[JCDAILYM2MPRICE] (
            [PRICEID] int NOT NULL,
            [VALIDDATE] date NULL,
            [COMMODITYID] varchar(30NULL,
            [PRICE] [DMONEY] NULL,
            [CURRENCYID] char(3NULL,
            [SHIPMONTHNUMBER] int NULL,
            CONSTRAINT [PK_JCDAILYM2MPRICE] PRIMARY KEY CLUSTERED ([PRICEID]));
          1.
          Run the basic query:
          select commodityid, price, shipmonthnumber
          from jcdailym2mprice 
          where validdate='2012-08-31' and commodityid='CR 10'    
          Get the result:

          commodityid

          price

          shipmonthnumber

          CR 10

          2615

          201209

          CR 10

          2615

          201210


          2.
          --hard code as research 1
          select commodityid, 
                case shipmonthnumber when 201209 then isnull(price, 0end as '201209',
                case shipmonthnumber when 201210 then isnull(price, 0end as '201210'
          from jcdailym2mprice 
          where validdate='2012-08-31' and commodityid='CR 10'
          Get the result:

          commodityid

          201209

          201210

          CR 10

          2615

          CR 10

          2615


          3.
          --hard code as research 2
          select  commodityid, sum([201209]as '201209'sum([201210]as '201210'
          from (select commodityid, 
                case shipmonthnumber when 201209 then isnull(price, 0end as '201209',
                case shipmonthnumber when 201210 then isnull(price, 0end as '201210'
                from jcdailym2mprice 
                where validdate='2012-08-31' and commodityid='CR 10') a 
          group by commodityid
          Get the result

          commodityid

          201209

          201210

          CR 10

          2615

          2615


          4.
          --use cursor to dynamic
          declare cur cursor for 
              select monthnumber, '['+convert(varchar(6), monthnumber)+']' from jcshipmonthsmapping 
              where monthnumber>convert(varchar(6), dateadd(month-2getdate()), 112
                and monthnumber<convert(varchar(6), dateadd(month11getdate()), 112)
          declare @yearmonth Integer@columnname varchar(max)
          declare @max varchar(max), @select varchar(max), @sql nvarchar(max)
          set @max=''
          set @select ='select commodityid '
          open cur
          fetch next from cur into @yearmonth,@columnname
          while @@fetch_status=0
          begin
              set @max=@max + ', max(' + @columnname + ') as ' + @columnname
              set @select=@select + ', case when shipmonthnumber=' + convert(varchar(6), @yearmonth+ ' then price end as '+ @columnname
              fetch next from cur into @yearmonth,@columnname
          end
          close cur
          deallocate cur
          set @sql = ' select commodityid ' + @max 
                    +' from (' + @select + ' from jcdailym2mprice where validdate=''2012-08-31'') a group by commodityid'
                   
          --print @sql
          exec sp_executesql @sql    
          Get the result:

          commodityid

          201209

          201210

          201211

          201212

          201301

          201302

          201303

          201304

          201305

          201306

          201307

          201308

          Brown Crepe 3X

          2650

          CR 10

          2615

          2615

          CR 20

          2610

          2610

          2610

          2610

          2620

          2620

          2620

          2630

          2630

          CSR 10

          2350

          CSR L

          2430

          2435

          SIR 20 S/T BSTN

          2513.2

          SIR 20 S/W

          2513.2

          2529.8

          2535.3

          2540.8

          SIR 20 UG SW

          2513.2

          2529.8

          2535.3

          2540.8

          SKIM BLOCK

          2300


          5.
          --pivot basic
          select commodityid, [201209][201210]
          from (
                select commodityid, price, shipmonthnumber from jcdailym2mprice where validdate='2012-08-31' and commodityid='CR 10'
                ) a 
          pivot ( sum(price) for shipmonthnumber in ([201209][201210])) as pvt
          Get the result

          commodityid

          201209

          201210

          CR 10

          2615

          2615


          6.
          --pivot dynamic
          declare cur cursor for 
          select '['+convert(varchar(6), monthnumber)+']' from jcshipmonthsmapping 
              where monthnumber>convert(varchar(6), dateadd(month-2getdate()), 112
                and monthnumber<convert(varchar(6), dateadd(month11getdate()), 112)
          declare @columns varchar(max), @subjects varchar(max), @sql nvarchar(max
          set @subjects = '[-1]'
          open cur
          fetch next from cur into @columns
          while @@fetch_status=0
          begin
          set @subjects = @subjects + '' + @columns
          fetch next from cur into @columns
          end
          close cur
          deallocate cur
          set @sql = ' select commodityid,' + @subjects 
                   + ' from ( select commodityid, price, shipmonthnumber from jcdailym2mprice where validdate=''2012-08-31'' and commodityid=''CR 10'') a'
                   + ' pivot(sum(price) for shipmonthnumber in ('+@subjects+')) as pvt'
          --print @sql        
          exec sp_executesql @sql
          Get the result

          commodityid

          -1

          201209

          201210

          201211

          201212

          201301

          201302

          201303

          201304

          201305

          201306

          201307

          CR 10

          2615

          2615



          posted on 2012-10-04 01:33 crazycy 閱讀(2120) 評論(1)  編輯  收藏 所屬分類: JavaEE技術DBMS

          評論

          # re: SQLSERVER: ROWS TO COLUMNS 行轉列 (二)   回復  更多評論   

          Hi Cui Yi

          FYI

          http://www.simple-talk.com/blogs/2007/09/14/pivots-with-dynamic-columns-in-sql-server-2005/
          2012-12-01 20:46 | Qiwen.jiang
          主站蜘蛛池模板: 蒙山县| 海原县| 醴陵市| 田东县| 罗山县| 庐江县| 德昌县| 宣汉县| 宣威市| 论坛| 九龙城区| 天峨县| 囊谦县| 军事| 府谷县| 北海市| 南丹县| 慈利县| 崇文区| 武山县| 阳原县| 红安县| 钟祥市| 高青县| 鄱阳县| 临猗县| 清水河县| 巴彦淖尔市| 龙南县| 江达县| 台前县| 南丰县| 离岛区| 华容县| 彰武县| 罗江县| 乌兰察布市| 额济纳旗| 隆安县| 磴口县| 东山县|