<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    cuiyi's blog(崔毅 crazycy)

    記錄點滴 鑒往事之得失 以資于發(fā)展
    數(shù)據(jù)加載中……

    SQLSERVER: ROWS TO COLUMNS 行轉(zhuǎn)列 (二)

    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 閱讀(2106) 評論(1)  編輯  收藏 所屬分類: JavaEE技術(shù)DBMS

    評論

    # re: SQLSERVER: ROWS TO COLUMNS 行轉(zhuǎn)列 (二)   回復(fù)  更多評論   

    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
    主站蜘蛛池模板: 国产乱码免费卡1卡二卡3卡| 狼色精品人妻在线视频免费| 天堂在线免费观看| 亚洲国产专区一区| 一级做受视频免费是看美女| 亚洲国产成人精品女人久久久 | 国产中文字幕免费| 亚洲a∨无码一区二区| 国产精品二区三区免费播放心| 欧洲亚洲国产精华液| 国产免费黄色大片| 一区二区在线视频免费观看| 亚洲中文字幕不卡无码| 青青操免费在线视频| 亚洲黄色在线电影| aa级一级天堂片免费观看| 亚洲久热无码av中文字幕| 又大又黄又粗又爽的免费视频| 特级毛片aaaa级毛片免费| 久久精品国产亚洲AV不卡| 高清一区二区三区免费视频| 666精品国产精品亚洲| 久久精品无码一区二区三区免费| 亚洲欧洲无码一区二区三区| 国产99视频免费精品是看6| 美女网站在线观看视频免费的| 婷婷久久久亚洲欧洲日产国码AV | 免费高清在线爱做视频| 日韩a毛片免费观看| 国产亚洲AV无码AV男人的天堂| 成人免费观看一区二区| 国产成人综合亚洲| 亚洲AV乱码一区二区三区林ゆな | 国产精品国产亚洲区艳妇糸列短篇| 亚洲无码视频在线| 亚洲精品在线免费观看视频 | 人妻在线日韩免费视频| 亚洲一级视频在线观看| 亚洲欧洲日产国码一级毛片| 18禁止看的免费污网站| 人人爽人人爽人人片A免费|