数据库表比如:table1是
日期         姓名
2012/7/20 张三
2012/7/21 张三
2012/7/20 李四
2012/7/20 王二
2012/7/24 麻子
2012/7/20 赵四
2012/7/26 王二
2012/7/27 麻子
想要的结果:
记录                 姓名
2012/7/20|2012/7/21 张三
2012/7/20         李四
2012/7/20|2012/7/26 王二
2012/7/24|2012/7/27 麻子
2012/7/20         赵四意思就是将相同的人名的日期字段合并在一起,中间用|连接在一起或者别的分隔符连接在一起都可以。然后输出。
是不是有点复杂啊?使用create table? insert into?谢谢!

解决方案 »

  1.   


    declare @t table (t varchar(10) ,n varchar(8))
    insert into @t
    select '2012/7/20', '张三' union all
    select '2012/7/21' ,'张三' union all
    select '2012/7/20' ,'李四' union all
    select '2012/7/20', '王二' union all
    select '2012/7/24' ,'麻子' union all
    select '2012/7/20', '赵四' union all
    select '2012/7/26', '王二' union all
    select '2012/7/27', '麻子'select * from @t
    select n, [ts]=stuff((select '|'+[t] from @t t where n=tb.n for xml path('')), 1, 1, '') 
    from @t tb 
    group by n /*(8 行受影响)
    t          n
    ---------- --------
    2012/7/20  张三
    2012/7/21  张三
    2012/7/20  李四
    2012/7/20  王二
    2012/7/24  麻子
    2012/7/20  赵四
    2012/7/26  王二
    2012/7/27  麻子(8 行受影响)n        ts
    -------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    李四       2012/7/20
    麻子       2012/7/24|2012/7/27
    王二       2012/7/20|2012/7/26
    张三       2012/7/20|2012/7/21
    赵四       2012/7/20(5 行受影响)*/
      

  2.   

    天呢,根本看不懂啊!但是可以用!你真神奇啊!
    [ts]=stuff((select '|'+[t] from @t t where n=tb.n for xml path('')), 1, 1, '') 
    这句话解释解释啊,或者给我一个搜索的方式。根本不懂啊。
      

  3.   


    这就是字符串拼接的一种常用方法 你可以搜索下SQL 字符串拼接
      

  4.   


    create table table1
    (日期 varchar(12), 姓名 varchar(8))insert into table1
    select '2012/7/20', '张三' union all
    select '2012/7/21', '张三' union all
    select '2012/7/20', '李四' union all
    select '2012/7/20', '王二' union all
    select '2012/7/24', '麻子' union all
    select '2012/7/20', '赵四' union all
    select '2012/7/26', '王二' union all
    select '2012/7/27', '麻子'
    create function dbo.fntable1(@xm varchar(12))
    returns varchar(25)
    as
    begin
      declare @result varchar(25)=''
      
      select @result=@result+'|'+日期 
      from table1
      where 姓名=@xm
      
      return stuff(@result,1,1,'')
    endselect dbo.fntable1(姓名) '记录',姓名
    from table1 group by 姓名/*
    记录                        姓名
    ------------------------- --------
    2012/7/20                 李四
    2012/7/24|2012/7/27       麻子
    2012/7/20|2012/7/26       王二
    2012/7/20|2012/7/21       张三
    2012/7/20                 赵四(5 row(s) affected)
    */
      

  5.   

    就是个列值合并--合并列值 
     --原著:邹建 
     --改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)  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')  
     go  
      
     create 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 id  
      
     drop 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 tb  
      
     drop 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中的方法 
     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 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 
      

  6.   

    前面几位已经很详细了,很久没练了,刚好看到,顺便写写create table test
    (
    d varchar(50),
    n varchar(50)
    )
    insert into test
    select '2012/7/20', '张三'
    union all
    select '2012/7/21', '张三'
    union all
    select '2012/7/20', '李四'
    union all
    select '2012/7/20', '王二'
    union all
    select '2012/7/24', '麻子'
    union all
    select '2012/7/20', '赵四'
    union all
    select '2012/7/26', '王二'
    union all
    select '2012/7/27', '麻子'GO
    create function z_test(@n varchar(50))
    returns varchar(max)
    as
    begin
    declare @sql varchar(max);
    set @sql='';
    select @sql=@sql+'|'+d from test where n=@n;
    return stuff(@sql,1,1,'');
    endselect distinct n,dbo.z_test(n) from test
      

  7.   


    select n,stuff((
    select '|'+d  from test t2 where t2.n=t1.n for xml path('')
    ),1,1,'')
     from test t1
     group by n附XML学习贴:http://www.cnblogs.com/yanghaibo/archive/2010/06/04/1751405.html