a bb 45HQ 23 23
a bb 20GP 23 23a cc 40GP 23 23
a cc 45HQ 23 23
a cc 20GP 23 23a dd 40GP 23 23
a dd 45HQ 23 23
a dd 20GP 23 23
上面是存储的数据,我希望得到的结果(第2列数据是动态的)
a cc/dd 40GP 23 23
a bb/cc/dd 45HQ 23 23
a bb/cc/dd 20GP 23 23

解决方案 »

  1.   

    --3.3.2 使用用户定义函数,配合SELECT处理完成字符串合并处理的示例
    --处理的数据
    CREATE TABLE tb(col1 varchar(10),col2 int)
    INSERT tb SELECT 'a',1
    UNION ALL SELECT 'a',2
    UNION ALL SELECT 'b',1
    UNION ALL SELECT 'b',2
    UNION ALL SELECT 'b',3
    GO--合并处理函数
    CREATE FUNCTION dbo.f_str(@col1 varchar(10))
    RETURNS varchar(100)
    AS
    BEGIN
    DECLARE @re varchar(100)
    SET @re=''
    SELECT @re=@re+','+CAST(col2 as varchar)
    FROM tb
    WHERE col1=@col1
    RETURN(STUFF(@re,1,1,''))
    END
    GO--调用函数
    SELECT col1,col2=dbo.f_str(col1) FROM tb GROUP BY col1
    --删除测试
    DROP TABLE tb
    DROP FUNCTION f_str
    /*--结果
    col1       col2
    ---------- -----------
    a          1,2
    b          1,2,3
    --*/
      

  2.   

    create table tb(c1 varchar(10),c2 varchar(10),c3 varchar(10),c4 int,c5 int)
    insert into tb values('a' ,'bb' ,'20GP' ,23 ,23) 
    insert into tb values('a' ,'cc' ,'40GP' ,23 ,23) 
    insert into tb values('a' ,'cc' ,'45HQ' ,23 ,23) 
    insert into tb values('a' ,'cc' ,'20GP' ,23 ,23) 
    insert into tb values('a' ,'dd' ,'40GP' ,23 ,23) 
    insert into tb values('a' ,'dd' ,'45HQ' ,23 ,23) 
    insert into tb values('a' ,'dd' ,'20GP' ,23 ,23)
    go--创建一个合并的函数
    create function f_hb(@c1 varchar(10) , @c3 varchar(10) , @c4 int , @c5 int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + '/' + cast(c2 as varchar) from tb where c1 = @c1 and c3 = @c3 and c4 = @c4 and c5 = @c5
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct c1,c3,c4,c5 ,dbo.f_hb(c1,c3,c4,c5) as c2 from tbdrop table tb
    drop function dbo.f_hb/*
    c1         c3         c4          c5          c2         
    ---------- ---------- ----------- ----------- -----------
    a          20GP       23          23          bb/cc/dd
    a          40GP       23          23          cc/dd
    a          45HQ       23          23          cc/dd(所影响的行数为 3 行)
    */
      

  3.   

    --上为2000中使用函数的方法,下有2005的方法.合并列值
    原著:邹建
    改编:爱新觉罗.毓华  2007-12-16  广东深圳表结构,数据如下:
    id    value
    ----- ------
    1     aa
    1     bb
    2     aaa
    2     bbb
    2     ccc需要得到结果:
    id     values
    ------ -----------
    1      aa,bb
    2      aaa,bbb,ccc
    即:group by id, 求 value 的和(字符串相加)1. 旧的解决方法(在sql server 2000中只能用函数解决。)
    --1. 创建处理函数
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    goCREATE FUNCTION dbo.f_str(@id int)
    RETURNS varchar(8000)
    AS
    BEGIN
        DECLARE @r varchar(8000)
        SET @r = ''
        SELECT @r = @r + ',' + value FROM tb WHERE id=@id
        RETURN STUFF(@r, 1, 1, '')
    END
    GO-- 调用函数
    SELECt id, value = dbo.f_str(id) FROM tb GROUP BY iddrop table tb
    drop function dbo.f_str/*
    id          value      
    ----------- -----------
    1           aa,bb
    2           aaa,bbb,ccc
    (所影响的行数为 2 行)
    */--2、另外一种函数.
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    go--创建一个合并的函数
    create function f_hb(@id int)
    returns varchar(8000)
    as
    begin
      declare @str varchar(8000)
      set @str = ''
      select @str = @str + ',' + cast(value as varchar) from tb where id = @id
      set @str = right(@str , len(@str) - 1)
      return(@str)
    End
    go--调用自定义函数得到结果:
    select distinct id ,dbo.f_hb(id) as value from tbdrop table tb
    drop function dbo.f_hb/*
    id          value      
    ----------- -----------
    1           aa,bb
    2           aaa,bbb,ccc
    (所影响的行数为 2 行)
    */2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    go
    -- 查询处理
    SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
            SELECT [values]= STUFF(REPLACE(REPLACE(
                (
                    SELECT value FROM tb N
                    WHERE id = A.id
                    FOR XML AUTO
                ), '<N value="', ','), '"/>', ''), 1, 1, '')
    )N
    drop table tb/*
    id          values
    ----------- -----------
    1           aa,bb
    2           aaa,bbb,ccc(2 行受影响)
    */--SQL2005中的方法2(csdn dobear_0922 提供)
    create table tb(id int, value varchar(10))
    insert into tb values(1, 'aa')
    insert into tb values(1, 'bb')
    insert into tb values(2, 'aaa')
    insert into tb values(2, 'bbb')
    insert into tb values(2, 'ccc')
    goselect id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
    from tb
    group by id/*
    id          values
    ----------- --------------------
    1           aa,bb
    2           aaa,bbb,ccc(2 row(s) affected)*/drop table tb
      

  4.   

    --> 测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (C1 varchar(1),C2 varchar(2),C3 varchar(4),C4 int,C5 int)
    insert into #T
    select 'a','bb','45HQ',23,23 union all
    select 'a','bb','20GP',23,23 union all
    select 'a','cc','40GP',23,23 union all
    select 'a','cc','45HQ',23,23 union all
    select 'a','cc','20GP',23,23 union all
    select 'a','dd','40GP',23,23 union all
    select 'a','dd','45HQ',23,23 union all
    select 'a','dd','20GP',23,23select C1, C2=(stuff((select '/'+C2 from #T where C3=a.C3 for xml path('')),1,1,'')),C3,C4,C5 from #T a group by C1,C3,C4,C5/*
    C1   C2        C3   C4   C5
    ---- --------- ---- ---- ----
    a    bb/cc/dd  20GP 23   23
    a    cc/dd     40GP 23   23
    a    bb/cc/dd  45HQ 23   23
    */