ID   QTY  REMARK
001   100  test1
001   200  test2
001   300  test3
002   120  test4
002   140  test5
002   150  test6结果:
001  600  test1,test2,test3
002  310  test4,test5,test6

解决方案 »

  1.   

    http://topic.csdn.net/u/20091013/15/9f058df7-4d29-47bf-a338-b63fcab2abc0.html?51371
      

  2.   

    if not object_id('Tab') is null
        drop table Tab
    Go
    Create table Tab([Col1] int,[Col2] nvarchar(1))
    Insert Tab
    select 1,N'a' union all
    select 1,N'b' union all
    select 1,N'c' union all
    select 2,N'd' union all
    select 2,N'e' union all
    select 3,N'f'
    Go合并表:SQL2000用函数:go
    if object_id('F_Str') is not null
        drop function F_Str
    go
    create function F_Str(@Col1 int)
    returns nvarchar(100)
    as
    begin
        declare @S nvarchar(100)
        select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
        return @S
    end
      

  3.   

    http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html?23514
      

  4.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-10-28 15:13:56
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([ID] varchar(3),[QTY] int,[REMARK] varchar(5))
    insert [tb]
    select '001',100,'test1' union all
    select '001',200,'test2' union all
    select '001',300,'test3' union all
    select '002',120,'test4' union all
    select '002',140,'test5' union all
    select '002',150,'test6'
    --------------开始查询--------------------------
    select 
      id, sum(QTY) as QTY ,[REMARK]=stuff((select ','+[REMARK] from tb t where id=tb.id for xml path('')), 1, 1, '') 
    from 
      tb 
    group by 
      id 
    ----------------结果----------------------------
    /*id   QTY         REMARK
    ---- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    001  600         test1,test2,test3
    002  410         test4,test5,test6(2 行受影响) 
    */
      

  5.   


    --数据
    create table tb (ID char(3), QTY int, REMARK nvarchar(20))
    insert into tb 
    select '001',  100 , 'test1' union all
    select '001',  200 , 'test2' union all
    select '001',  300 , 'test3' union all
    select '002',  120 , 'test4' union all
    select '002',  140 , 'test5' union all
    select '002',  150 , 'test6' 
    --函数
    create function F_Str(@Col1 int)
    returns nvarchar(100)
    as
    begin
        declare @S nvarchar(100)
        select @S=isnull(@S+',','')+REMARK from tb where id=@Col1
        return @S
    end
    --查询
    select ID,sum(QTY),dbo.F_Str(ID)
    from tb
    group by ID
    drop table tb
    --结果
    ----------------------
    001 600 test1,test2,test3
    002 410 test4,test5,test6