日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴(lài)的軟件下載站!
      軟件
      軟件
      文章
      搜索

      首頁(yè)西西教程數(shù)據(jù)庫(kù)教程 → sql 行列互換

      sql 行列互換

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站整理時(shí)間:2011/1/1 22:08:08字體大小:A-A+

      作者:佚名點(diǎn)擊:192次評(píng)論:1次標(biāo)簽: sql 數(shù)據(jù)處理

      • 類(lèi)型:文件處理大。204KB語(yǔ)言:中文 評(píng)分:5.0
      • 標(biāo)簽:
      立即下載

       sql 行列互換
      --行列互換

      /*--有表
      indust 200301 200302 200303
      ---------- ---------- ---------- ----------
      a 111 222 333
      b 444 555 666
      c 777 888 999
      d 789 910 012


      --要求得到結(jié)果
      日期 a b c d
      ------ ---- ---- ---- ----
      200301 111 444 777 789
      200302 222 555 888 910
      200303 333 666 999 012
      --*/

      --創(chuàng)建測(cè)試表
      create table test(indust varchar(10)
      ,[200301] varchar(10)
      ,[200302] varchar(10)
      ,[200303] varchar(10))
      insert test select 'a','111','222','333'
      union all select 'b','444','555','666'
      union all select 'c','777','888','999'
      union all select 'd','789','910','012'
      go

      --數(shù)據(jù)處理
      declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000)
      select @f1='',@f2='',@f3=''
      select @f1=@f1+',['+indust+']='''+[200301]+''''
      ,@f2=@f2+','''+[200302]+''''
      ,@f3=@f3+','''+[200303]+''''
      from test
      exec('select 日期=''200301'''+@f1
      +' union all select ''200302'''+@f2
      +' union all select ''200303'''+@f3)
      go
      --刪除測(cè)試表
      select * from test
      drop table test

      /*--測(cè)試結(jié)果
      日期 a b c d
      ------ ---- ---- ---- ----
      200301 111 444 777 789
      200302 222 555 888 910
      200303 333 666 999 012
      --*/

      -------------------------------------------------------------------------------------

      --行列轉(zhuǎn)換示例

      --測(cè)試數(shù)據(jù)
      create table 表([11] varchar(2),[22] int,[33] int,[44] int,[55] int)
      insert 表 select 'aa',1,2,3,6
      union all select 'bb',0,1,3,5
      union all select 'cc',1,2,3,6
      union all select 'dd',1,2,3,6
      union all select 'ee',1,2,3,6
      go

      --查詢處理
      declare @s1 varchar(8000),@s2 varchar(8000)
      ,@s3 varchar(8000),@s4 varchar(8000),@s5 varchar(8000)
      ,@i varchar(10)
      select @s1='',@s2='',@s3='',@s4='',@s5='',@i='0'
      select @s1=@s1+',@'+@i+' varchar(8000)'
      ,@s2=@s2+',@'+@i+'='''''
      ,@s3=@s3+'
      select @'+@i+'=@'+@i+'+'',[''+[11]+'']=''+cast(['+name+'] as varchar) from 表'
      ,@s4=@s4+',@'+@i+'=''select ''+substring(@'+@i+',2,8000)'
      ,@s5=@s5+'+'' union all ''+@'+@i
      ,@i=cast(@i as int)+1
      from syscolumns
      where object_id('表')=id and colid<>1

      select @s1=substring(@s1,2,8000)
      ,@s2=substring(@s2,2,8000)
      ,@s4=substring(@s4,2,8000)
      ,@s5=substring(@s5,16,8000)

      exec('declare '+@s1+'
      select '+@s2+@s3+'
      select '+@s4+'
      exec('+@s5+')')
      go

      --刪除測(cè)試表
      drop table 表

      /*--測(cè)試結(jié)果
      aa bb cc dd ee
      ----------- ----------- ----------- ----------- -----------
      1 0 1 1 1
      2 1 2 2 2
      3 3 3 3 3
      6 5 6 6 6

      (所影響的行數(shù)為 4 行)

      codenames type flag
      zhao a y
      zhao b n
      zhao c y
      li a n
      li b y
      li c y
      查詢出來(lái)為

      names typea flaga typeb falgb typec falgc
      zhao a y b n c y
      li a n b y c n

      declare @sql varchar(8000)
      set @sql='select names'
      select @sql=@sql+',max(case when type='''+type+''' then type end) [type'+type+']'
      +',max(case when type='''+type+''' then flag end) [flag'+type+']'
      from tb group by type

      exec (@sql+' from tb group by names')

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過(guò)難過(guò)
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門(mén)評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(1)

        昵稱(chēng):
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)