[pcmc]          [zt]   [sbcount]
2007年第1批 报部审批 10
2007年第1批 回复发证 252
2007年第1批 网上公告 74
2007年第2批 报部审批 6
2007年第2批 初审上报 3
2007年第2批 回复发证 2
2007年第3批 初审上报 4
2007年第4批 报部审批 2
2007年第4批 初审上报 3
2007年第6批 初审上报 2
2007年第6批 回复发证 4
2007年第7批 初审上报 1
2007年第7批 回复发证 1
2007年第8批 初审上报 2
2007年第9批 初审上报 7
2007年第10批 报部审批 1
2007年第10批 初审上报 4
2007年第11批 初审上报 2
2007年第12批 初审上报 15
2007年第13批 初审上报 30
2007年第14批 报部审批 3
2007年第14批 初审上报 11
2007年第15批 初审上报 20
2007年第16批 初审上报 7
2007年第17批 初审上报 27
2007年第18批 互校 6
2007年第18批 自校 29
2007年第0批 回复发证 3
2007年第19批 自校 2
============================
sql怎么得到结果集
2007年第1批 10项报部审批,252项回复发证,74项网上公告
2007年第2批 6项报部审批,3项初审上报,2项回复发证
2007年第3批 4项初审上报
2007年第4批 2项报部审批,3项初审上报
2007年第6批 2项初审上报,4项回复发证
2007年第7批 1项初审上报,1项回复发证
2007年第8批 2项初审上报
2007年第9批 7项初审上报
2007年第10批 1项报部审批,4项初审上报
2007年第11批 2项初审上报
2007年第12批 15项初审上报
2007年第13批 30项初审上报
2007年第14批 3项报部审批,11项初审上报
2007年第15批 20项初审上报
2007年第16批 7项初审上报
2007年第17批 27项初审上报
2007年第18批 6项互校,30项自校
2007年第0批 3项回复发证
2007年第19批 2项自校

解决方案 »

  1.   

    --带符号合并行列转换--有表t,其数据如下:
      a b
      1 1
      1 2
      1 3
      2 1
      2 2
      3 1
    --如何转换成如下结果:
      a b
      1 1,2,3
      2 1,2
      3 1 create table tb
    (
       a int,
       b int
    )
    insert into tb(a,b) values(1,1)
    insert into tb(a,b) values(1,2)
    insert into tb(a,b) values(1,3)
    insert into tb(a,b) values(2,1)
    insert into tb(a,b) values(2,2)
    insert into tb(a,b) values(3,1)
    goif object_id('pubs..f_hb') is not null
       drop function f_hb
    go--创建一个合并的函数
    create function f_hb(@a int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(b as varchar) from tb where a = @a 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct a ,dbo.f_hb(a) as b from tbdrop table tb
      

  2.   

    create table tb(pcmc varchar(20),zt varchar(20),sbcount int)
    insert into tb values('2007年第1批' ,'报部审批', 10) 
    insert into tb values('2007年第1批' ,'回复发证', 252) 
    insert into tb values('2007年第1批' ,'网上公告', 74 )
    insert into tb values('2007年第2批' ,'报部审批', 6 )
    insert into tb values('2007年第2批' ,'初审上报', 3 )
    insert into tb values('2007年第2批' ,'回复发证', 2 )
    insert into tb values('2007年第3批' ,'初审上报', 4 )
    insert into tb values('2007年第4批' ,'报部审批', 2 )
    insert into tb values('2007年第4批' ,'初审上报', 3 )
    insert into tb values('2007年第6批' ,'初审上报', 2 )
    insert into tb values('2007年第6批' ,'回复发证', 4 )
    insert into tb values('2007年第7批' ,'初审上报', 1 )
    insert into tb values('2007年第7批' ,'回复发证', 1 )
    insert into tb values('2007年第8批' ,'初审上报', 2 )
    insert into tb values('2007年第9批' ,'初审上报' ,7 )
    insert into tb values('2007年第10批', '报部审批', 1 )
    insert into tb values('2007年第10批', '初审上报', 4 )
    insert into tb values('2007年第11批', '初审上报', 2 )
    insert into tb values('2007年第12批', '初审上报', 15 )
    insert into tb values('2007年第13批', '初审上报', 30 )
    insert into tb values('2007年第14批', '报部审批', 3 )
    insert into tb values('2007年第14批', '初审上报', 11 )
    insert into tb values('2007年第15批', '初审上报', 20 )
    insert into tb values('2007年第16批', '初审上报', 7 )
    insert into tb values('2007年第17批', '初审上报', 27) 
    insert into tb values('2007年第18批', '互校' ,6 )
    insert into tb values('2007年第18批', '自校' ,29 )
    insert into tb values('2007年第0批' ,'回复发证', 3) 
    insert into tb values('2007年第19批', '自校' ,2) 
    go--创建一个合并的函数
    create function f_hb(@pcmc varchar(20))
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(sbcount as varchar) + '项'+ cast(zt as varchar) from tb where pcmc = @pcmc 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct pcmc ,dbo.f_hb(pcmc) as 结果集 from tb order by pcmcdrop table tb
    drop function f_hb/*
    pcmc                 结果集                     
    -------------------- ---------------------------
    2007年第0批             3项回复发证
    2007年第10批            1项报部审批,4项初审上报
    2007年第11批            2项初审上报
    2007年第12批            15项初审上报
    2007年第13批            30项初审上报
    2007年第14批            3项报部审批,11项初审上报
    2007年第15批            20项初审上报
    2007年第16批            7项初审上报
    2007年第17批            27项初审上报
    2007年第18批            6项互校,29项自校
    2007年第19批            2项自校
    2007年第1批             10项报部审批,252项回复发证,74项网上公告
    2007年第2批             6项报部审批,3项初审上报,2项回复发证
    2007年第3批             4项初审上报
    2007年第4批             2项报部审批,3项初审上报
    2007年第6批             2项初审上报,4项回复发证
    2007年第7批             1项初审上报,1项回复发证
    2007年第8批             2项初审上报
    2007年第9批             7项初审上报(所影响的行数为 19 行)*/
      

  3.   


    CREATE  FUNCTION  DBO.test   
    ( @pcmc  VARCHAR(20)) RETURNS  VARCHAR(2000)
    ASBEGIN DECLARE @P_STR  VARCHAR(500)  SET  @P_STR=''
    SELECT  @P_STR=@P_STR+','+ sbcount +zt  FROM TABLEA
    SELECT    @P_STR=RIGHT(@P_STR,LEN(@P_STR)-1)  

    RETURN  @P_STRENDselect DISTINCT pcmc ,DBO.TEST(pcmc)
    from tableA
      

  4.   

    create table tb(pcmc varchar(20),zt varchar(20),sbcount int)
    insert into tb values('2007年第1批' ,'报部审批', 10) 
    insert into tb values('2007年第1批' ,'回复发证', 252) 
    insert into tb values('2007年第1批' ,'网上公告', 74 )
    insert into tb values('2007年第2批' ,'报部审批', 6 )
    insert into tb values('2007年第2批' ,'初审上报', 3 )
    insert into tb values('2007年第2批' ,'回复发证', 2 )
    insert into tb values('2007年第3批' ,'初审上报', 4 )
    insert into tb values('2007年第4批' ,'报部审批', 2 )
    insert into tb values('2007年第4批' ,'初审上报', 3 )
    insert into tb values('2007年第6批' ,'初审上报', 2 )
    insert into tb values('2007年第6批' ,'回复发证', 4 )
    insert into tb values('2007年第7批' ,'初审上报', 1 )
    insert into tb values('2007年第7批' ,'回复发证', 1 )
    insert into tb values('2007年第8批' ,'初审上报', 2 )
    insert into tb values('2007年第9批' ,'初审上报' ,7 )
    insert into tb values('2007年第10批', '报部审批', 1 )
    insert into tb values('2007年第10批', '初审上报', 4 )
    insert into tb values('2007年第11批', '初审上报', 2 )
    insert into tb values('2007年第12批', '初审上报', 15 )
    insert into tb values('2007年第13批', '初审上报', 30 )
    insert into tb values('2007年第14批', '报部审批', 3 )
    insert into tb values('2007年第14批', '初审上报', 11 )
    insert into tb values('2007年第15批', '初审上报', 20 )
    insert into tb values('2007年第16批', '初审上报', 7 )
    insert into tb values('2007年第17批', '初审上报', 27) 
    insert into tb values('2007年第18批', '互校' ,6 )
    insert into tb values('2007年第18批', '自校' ,29 )
    insert into tb values('2007年第0批' ,'回复发证', 3) 
    insert into tb values('2007年第19批', '自校' ,2) 
    go--创建一个合并的函数
    create function f_hb(@pcmc varchar(20))
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(sbcount as varchar) + '项'+ cast(zt as varchar) from tb where pcmc = @pcmc 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select 结果集 = pcmc + ' ' + 结果集 from
    (
      select distinct pcmc  , dbo.f_hb(pcmc) as 结果集 from tb 
    ) tdrop table tb
    drop function f_hb/*
    结果集                             
    -----------------------------------
    2007年第0批 3项回复发证
    2007年第10批 1项报部审批,4项初审上报
    2007年第11批 2项初审上报
    2007年第12批 15项初审上报
    2007年第13批 30项初审上报
    2007年第14批 3项报部审批,11项初审上报
    2007年第15批 20项初审上报
    2007年第16批 7项初审上报
    2007年第17批 27项初审上报
    2007年第18批 6项互校,29项自校
    2007年第19批 2项自校
    2007年第1批 10项报部审批,252项回复发证,74项网上公告
    2007年第2批 6项报部审批,3项初审上报,2项回复发证
    2007年第3批 4项初审上报
    2007年第4批 2项报部审批,3项初审上报
    2007年第6批 2项初审上报,4项回复发证
    2007年第7批 1项初审上报,1项回复发证
    2007年第8批 2项初审上报
    2007年第9批 7项初审上报(所影响的行数为 19 行)
    */
      

  5.   

    create table tb(pcmc varchar(20),zt varchar(20),sbcount int)
    insert into tb values('2007年第1批' ,'报部审批', 10) 
    insert into tb values('2007年第1批' ,'回复发证', 252) 
    insert into tb values('2007年第1批' ,'网上公告', 74 )
    insert into tb values('2007年第2批' ,'报部审批', 6 )
    insert into tb values('2007年第2批' ,'初审上报', 3 )
    insert into tb values('2007年第2批' ,'回复发证', 2 )
    insert into tb values('2007年第3批' ,'初审上报', 4 )
    insert into tb values('2007年第4批' ,'报部审批', 2 )
    insert into tb values('2007年第4批' ,'初审上报', 3 )
    insert into tb values('2007年第6批' ,'初审上报', 2 )
    insert into tb values('2007年第6批' ,'回复发证', 4 )
    insert into tb values('2007年第7批' ,'初审上报', 1 )
    insert into tb values('2007年第7批' ,'回复发证', 1 )
    insert into tb values('2007年第8批' ,'初审上报', 2 )
    insert into tb values('2007年第9批' ,'初审上报' ,7 )
    insert into tb values('2007年第10批', '报部审批', 1 )
    insert into tb values('2007年第10批', '初审上报', 4 )
    insert into tb values('2007年第11批', '初审上报', 2 )
    insert into tb values('2007年第12批', '初审上报', 15 )
    insert into tb values('2007年第13批', '初审上报', 30 )
    insert into tb values('2007年第14批', '报部审批', 3 )
    insert into tb values('2007年第14批', '初审上报', 11 )
    insert into tb values('2007年第15批', '初审上报', 20 )
    insert into tb values('2007年第16批', '初审上报', 7 )
    insert into tb values('2007年第17批', '初审上报', 27) 
    insert into tb values('2007年第18批', '互校' ,6 )
    insert into tb values('2007年第18批', '自校' ,29 )
    insert into tb values('2007年第0批' ,'回复发证', 3) 
    insert into tb values('2007年第19批', '自校' ,2) 
    go--创建一个合并的函数
    create function f_hb(@pcmc varchar(20))
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(sbcount as varchar) + '项'+ cast(zt as varchar) from tb where pcmc = @pcmc 
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select 结果集 = pcmc + ' ' + 结果集 from
    (
      select distinct pcmc  , dbo.f_hb(pcmc) as 结果集 from tb 
    ) t
    order by cast(substring(pcmc , charindex('第',pcmc)+1 , charindex('批',pcmc) -charindex('第',pcmc)-1) as int)drop table tb
    drop function f_hb/*
    结果集                 
    -----------------------
    2007年第0批 3项回复发证
    2007年第1批 10项报部审批,252项回复发证,74项网上公告
    2007年第2批 6项报部审批,3项初审上报,2项回复发证
    2007年第3批 4项初审上报
    2007年第4批 2项报部审批,3项初审上报
    2007年第6批 2项初审上报,4项回复发证
    2007年第7批 1项初审上报,1项回复发证
    2007年第8批 2项初审上报
    2007年第9批 7项初审上报
    2007年第10批 1项报部审批,4项初审上报
    2007年第11批 2项初审上报
    2007年第12批 15项初审上报
    2007年第13批 30项初审上报
    2007年第14批 3项报部审批,11项初审上报
    2007年第15批 20项初审上报
    2007年第16批 7项初审上报
    2007年第17批 27项初审上报
    2007年第18批 6项互校,29项自校
    2007年第19批 2项自校(所影响的行数为 19 行)
    */
      

  6.   

    create table tb(pcmc varchar(20),zt varchar(20),sbcount int)
    insert into tb values('2007年第1批' ,'报部审批', 10) 
    insert into tb values('2007年第1批' ,'回复发证', 252) 
    insert into tb values('2007年第1批' ,'网上公告', 74 )
    insert into tb values('2007年第2批' ,'报部审批', 6 )
    insert into tb values('2007年第2批' ,'初审上报', 3 )
    insert into tb values('2007年第2批' ,'回复发证', 2 )
    insert into tb values('2007年第3批' ,'初审上报', 4 )
    insert into tb values('2007年第4批' ,'报部审批', 2 )
    insert into tb values('2007年第4批' ,'初审上报', 3 )
    insert into tb values('2007年第6批' ,'初审上报', 2 )
    insert into tb values('2007年第6批' ,'回复发证', 4 )
    insert into tb values('2007年第7批' ,'初审上报', 1 )
    insert into tb values('2007年第7批' ,'回复发证', 1 )
    insert into tb values('2007年第8批' ,'初审上报', 2 )
    insert into tb values('2007年第9批' ,'初审上报' ,7 )
    insert into tb values('2007年第10批', '报部审批', 1 )
    insert into tb values('2007年第10批', '初审上报', 4 )
    insert into tb values('2007年第11批', '初审上报', 2 )
    insert into tb values('2007年第12批', '初审上报', 15 )
    insert into tb values('2007年第13批', '初审上报', 30 )
    insert into tb values('2007年第14批', '报部审批', 3 )
    insert into tb values('2007年第14批', '初审上报', 11 )
    insert into tb values('2007年第15批', '初审上报', 20 )
    insert into tb values('2007年第16批', '初审上报', 7 )
    insert into tb values('2007年第17批', '初审上报', 27) 
    insert into tb values('2007年第18批', '互校' ,6 )
    insert into tb values('2007年第18批', '自校' ,29 )
    insert into tb values('2007年第0批' ,'回复发证', 3) 
    insert into tb values('2007年第19批', '自校' ,2) 
    go
    create function sss(@pcmc varchar(20))
    returns varchar(1000)
    as
    begin
    declare @sql varchar(1000)
    select @sql=isnull(@sql+',','')+cast(sbcount as varchar)+'项'+zt from tb where pcmc=@pcmc
    return @sql
    endselect distinct pcmc,dbo.sss(pcmc) from tb