有一个表temp,数据如下所示:
ID(key)   Code    Name  
1          001    名称101
2          001    名称102
3          002    名称201
4          003    名称301想得到以下的检索结果
001    名称101;名称102
002    名称201
003    名称301想要实现的功能是:Code相同的记录,就把Name合并到一起。
这样的检索语句应该怎么写?谢谢各位大虾!

解决方案 »

  1.   

    参考:--生成测试数据
    create table 表(部门 int,人员 varchar(20))
    insert into 表 select 1,'张三'
    insert into 表 select 1,'李四'
    insert into 表 select 1,'王五'
    insert into 表 select 2,'赵六'
    insert into 表 select 2,'邓七'
    insert into 表 select 2,'刘八'
    go--创建用户定义函数
    create function f_str(@department int)
    returns varchar(8000)
    as
    begin
        declare @ret varchar(8000)
        set @ret = ''
        select @ret = @ret+','+人员 from 表 where 部门 = @department
        set @ret = stuff(@ret,1,1,'')
        return @ret 
    end
    go
    --执行
    select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
    go--输出结果
    /*
    部门  人员
    ----  --------------
    1     张三,李四,王五
    2     赵六,邓七,刘八
    */
    --删除测试数据
    drop function f_str
    drop table 表
    go
      

  2.   


    create table tb(id int,code varchar(100),Name varchar(100))
    go
    insert into tb
    select 1,'001','名称101' union all
    select 1,'001','名称102' union all
    select 2,'002','名称201' union all
    select 3,'003','名称301' go
    --写一个聚合函数:
    create function dbo.fn_Merge(@id int)
    returns varchar(8000)
    as
    begin
       declare @r varchar(8000)
       set @r=''
       select @r=@r+';'+Name from tb where Code=@id
       return stuff(@r,1,1,'')
    end
    go-- 调用函数
    select id, dbo.fn_Merge(id) as name from tb group by id
      
    go
    drop table tb
    drop function fn_Merge
      

  3.   

    --生成测试数据
    create table temp(ID int,Code varchar(4),Name varchar(20))
    insert into temp select 1,'001','名称101'
    insert into temp select 2,'001','名称102'
    insert into temp select 3,'002','名称201'
    insert into temp select 4,'003','名称301'
    go--创建用户定义函数
    create function f_str(@Code int)
    returns varchar(8000)
    as
    begin
        declare @ret varchar(8000)
        set @ret = ''
        select @ret = @ret+';'+Name from temp where Code = @Code
        set @ret = stuff(@ret,1,1,'')
        return @ret 
    end
    go--执行
    select Code,Name=dbo.f_str(Code) from temp group by Code order by Code
    go
    --输出结果
    /*
    Code Name
    ---- -------------------
    001  名称101;名称102
    002  名称201
    003  名称301
    */--删除测试数据
    drop function f_str
    drop table temp
    go
      

  4.   

    --改一下
    create table tb(id int,code varchar(100),Name varchar(100))
    go
    insert into tb
    select 1,'001','名称101' union all
    select 1,'001','名称102' union all
    select 2,'002','名称201' union all
    select 3,'003','名称301' go
    --写一个聚合函数:
    create function dbo.fn_Merge(@id int)
    returns varchar(8000)
    as
    begin
       declare @r varchar(8000)
       set @r=''
       select @r=@r+';'+Name from tb where Code=@id
       return stuff(@r,1,1,'')
    end
    go-- 调用函数
    select code, dbo.fn_Merge(code) as name from tb group by code
      
    go
    drop table tb
    drop function fn_Merge
      

  5.   

    --需要一个合并函数create function fn_Names(
    @Code varchar(10)

    returns varchar(300)
    as
    begin
      declare @r varchar(300)
      set @r=''
      select @r=@r+';'+name from temp where code=@code
      if @r<>''
        set @r=stuff(@r,1,1,'')
      return @r
    endgo--调用
    select code,dbo.fn_Names(code) as Name
    from tmp
    group by code
      

  6.   

    -- sql 2005 可以直接这样SELECT *
    FROM(
    SELECT DISTINCT
    code
    FROM tb
    )A
    CROSS APPLY(
    SELECT Re = STUFF((
    SELECT v = Name
    FROM tb t
    WHERE code = A.code
    FOR XML AUTO, TYPE
    ).query('<r>
    {for $i in /t/@v return concat(",",string($i))}
    </r>').value('/r[1]','nvarchar(max)'), 1, 1, '')
    )B
      

  7.   

    有没有用一条SQL语句就可以实现的