国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

行列轉換,交叉表 (統計用,支持動態類別的SQL)

2024-07-21 02:45:21
字體:
來源:轉載
供稿:網友

這段時間在看SAAS方面的書籍時發現一個關于數據處理的問題。里面講到一種擴展性非常好的方法。但一直到今天終于在網上找到了。只可惜現在還在探索,只是找到一個例子而已。我在這里把共享希望對大家有用。下面的語句在sql server 2005中我測試過沒有問題。

1: 列轉為行:
eg1:
Create table test (name char(10),km char(10),cj int)
go
insert test values('張三','語文',80)
insert test values('張三','數學',86)
insert test values('張三','英語',75)
insert test values('李四','語文',78)
insert test values('李四','數學',85)
insert test values('李四','英語',78)
想變成
姓名      語文      數學      英語
張三      80        86        75
李四      78        85        78

declare @sql varchar(8000)
set @sql = 'select name'
select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
from (select distinct km from test) as a
select @sql = @sql+' from test group by name'
exec(@sql)
drop table test
eg2:
有表A,
id pid
1      1
1      2
1      3
2      1
2      2
3      1
如何化成表B:
id pid
     1     1,2,3
     2     1,2
     3     1
或者是從表B變成A(不要用游標)
以前有相似的列子,現在找不到了,幫幫忙!

--1.創建一個合并的函數
create function fmerg(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(pid as varchar) from 表A where id=@id
set @str=right(@str,len(@str)-1)
return(@str)
End
go
--調用自定義函數得到結果
select distinct id,dbo.fmerg(id) from 表A

2:
/***********        行轉列      *****************/
測試:
create table t1 (a int,b int,c int,d int,e int,f int,g int,h int)
insert t1 values(15, 9, 1, 0, 1, 2, 2, 0)
declare @ varchar(8000)
set @=''
select @=@+rtrim(name)+' from t1 union all select ' from syscolumns where id=object_id('t1')
set @=left(@,len(@)-len(' from t1 union all select '))
--PRint @
exec('select '+@+' from t1')
a          
-----------
15
9
1
0
1
2
2
0
===============================================================================
                以下個帶出的問題:
===============================================================================


3.將結果矩陣轉置
if      exists      (select      *      from      sysobjects      where      id      =      object_id('proc_sky_blue')      and      xtype      ='P')
drop      proc      proc_sky_blue
go
create      proc      proc_sky_blue      (@tablename      varchar(200))
as
begin
       set      nocount      on
       declare      @col      nvarchar(256)
       declare      @makesql      nvarchar(4000)
       declare      @insertsql      nvarchar(4000)
       declare      @caculatesql      nvarchar(400)
       declare      @count      int
       declare      @i      int
       create      table      #tmp      (colname      nvarchar(20))
       select      @caculatesql      =      'select      @count=count(1)      from      '      +      @tablename
       exec      sp_executesql      @caculatesql,      N'@count      int      output',@count      output
       if      @count      >=1024
       begin
           raiserror('表的行數太多了,我轉不了',16,1)
       end
       else
       begin
           select      @i=0
           while      @count      >0
           begin
               select      @i=@i+1
               select      @makesql      =      'alter      table      #tmp      add      col'+convert(varchar(20),@i)+'      int'
               exec(@makesql)
               select      @count=@count-1        
           end
           declare      my_cursor      cursor      for  
           select      name      from      syscolumns      where      id=object_id(@tablename)      order      by      colid
           open      my_cursor
           fetch      next      from      my_cursor      into      @col
           while      @@fetch_status      =      0
           begin
               select      @makesql      ='select      @insertsql=      @insertsql      +      convert(varchar(4),'+@col+')      +'',''      from      '      +@tablename
               select      @insertsql      =N'insert      #tmp      values      ('''+@col+      ''','
               execute      sp_executesql      @makesql,N'@insertsql      nvarchar(4000)      output'      ,@insertsql      output
               select      @insertsql      =      left(@insertsql,len(@insertsql)-1)      +')'
               exec(@insertsql)
               fetch      next      from      my_cursor      into      @col
           end
           close      my_cursor
           deallocate      my_cursor
           select      *      from      #tmp
           set      nocount      off
       end
end

go
----------------分析
declare      @tablename      varchar(200)
set      @tablename='table1'
begin
       set      nocount      on
       declare      @col      nvarchar(256)
       declare      @makesql      nvarchar(4000)
       declare      @insertsql      nvarchar(4000)
       declare      @caculatesql      nvarchar(400)
       declare      @count      int
       declare      @i      int
       create      table      #tmp      (colname      nvarchar(20))
       select      @caculatesql      =      'select      @count=count(1)      from      '      +      @tablename
       exec      sp_executesql      @caculatesql,      N'@count      int      output',@count      output
       if      @count      >=1024
       begin
           raiserror('表的行數太多了,我轉不了',16,1)
       end
       else
       begin
           select      @i=0
           while      @count      >0
           begin
               select      @i=@i+1
               select      @makesql      =      'alter      table      #tmp      add      col'+convert(varchar(20),@i)+'      int'
               exec(@makesql)
               select      @count=@count-1        
           end
           declare      my_cursor      cursor      for  
           select      name      from      syscolumns      where      id=object_id(@tablename)      order      by      colid
           open      my_cursor
           fetch      next      from      my_cursor      into      @col
           while      @@fetch_status      =      0
           begin
               select      @makesql      ='select      @insertsql=      @insertsql      +      convert(varchar(4),'+@col+')      +'',''      from      '      +@tablename
               select      @insertsql      =N'insert      #tmp      values      ('''+@col+      ''','
               execute      sp_executesql      @makesql,N'@insertsql      nvarchar(4000)      output'      ,@insertsql      output
               select      @insertsql      =      left(@insertsql,len(@insertsql)-1)      +')'
               select      @insertsql
               --exec(@insertsql)
               fetch      next      from      my_cursor      into      @col
           end
           close      my_cursor
           deallocate      my_cursor
           select      *      from      #tmp
           set      nocount      off
           drop      table      #tmp


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泊头市| 灌南县| 河源市| 桃江县| 锦屏县| 延川县| 南投县| 镇宁| 搜索| 萝北县| 南宁市| 门源| 莱芜市| 墨江| 府谷县| 满洲里市| 廊坊市| 平顺县| 大石桥市| 岗巴县| 稻城县| 永康市| 平南县| 绍兴县| 昌都县| 通州区| 高邮市| 屏山县| 尼木县| 喀什市| 调兵山市| 霞浦县| 新巴尔虎左旗| 肃南| 华蓥市| 睢宁县| 高安市| 安龙县| 乌什县| 迁安市| 洪江市|