寫個合併函數。eg:--合併函數Create Table TEST
(aaa Int,
bbbb Int,
amount Int)
Insert TEST Select 111, 222, 333
Union All Select 222, 222, 444
Union All Select 111, 333, 555 
Union All Select 111, 222, 777 
GO
Create Function Getamount(@aaa Int, @bbbb Int)
Returns Varchar(8000)
As
Begin
Declare @S Varchar(8000)
Select @S = ''
Select @S = @S + ',' + Rtrim(amount) From TEST Where aaa = @aaa And bbbb = @bbbb
Select @S = Stuff(@S , 1, 1 ,'')
Return @S
End
GO
Select
aaa,
bbbb,
dbo.Getamount(aaa, bbbb) As amount
From 
TEST
Group By aaa, bbbb
GO
Drop Table TEST
Drop Function Getamount
--Result
/*
aaa bbbb amount
111 222 333,777
111 333 555
222 222 444
*/

解决方案 »

  1.   


    --合併函數Create Table TEST
    (ID Int,
    Name Nvarchar(10))
    Insert TEST Select 510, N'计算机'
    Union All Select 511, N'计算机'
    Union All Select 512, N'计算机'
    Union All Select 512, N'摄像机'
    Union All Select 512, N'相机'
    Union All Select 513, N'U盘'
    Union All Select 514, N'U盘'
    Union All Select 515, N'计算机'
    GO
    Create Function GetName(@ID Int)
    Returns Nvarchar(4000)
    As
    Begin
    Declare @S Nvarchar(4000)
    Select @S = ''
    Select @S = @S + ',' + Rtrim(Name) From TEST Where ID = @ID
    Select @S = Stuff(@S , 1, 1 ,'')
    Return @S
    End
    GO
    Select
    ID,
    dbo.GetName(ID) As Name
    From 
    TEST
    Group By 
    ID
    GO
    Drop Table TEST
    Drop Function GetName
    --Result
    /*
    ID Name
    510 计算机
    511 计算机
    512 计算机,摄像机,相机
    513 U盘
    514 U盘
    515 计算机
    */
      

  2.   

    在SQL2000中只能使用函數實現,如果是SQL2005,就可以不用函數。參考此貼中老大的寫法。http://community.csdn.net/Expert/topic/4800/4800752.xml?temp=.9653742
      

  3.   

    Create Table t(id Int,c2 Int)
    Insert t Select 510, '计算机'
    Union All Select 511, '计算机'
    Union All Select 512, '计算机'
    Union All Select 512, '摄像机'
    Union All Select 512, '相机'
    Union All Select 513, 'U盘'
    Union All Select 514, 'U盘'
    Union All Select 515, '摄像机'
    create Function f1(@id Int)
    Returns Varchar(8000)
    As
    Begin
    Declare @str Varchar(8000)
    Select @str = ''
    Select @str = @str + ',' + Rtrim(c2) From t Where id = @id
    Select @str = Stuff(@str , 1, 1 ,'')
    Return @str
    End
    GO
    Select id dbo.f1(id)
    From t
    Group By id
      

  4.   

    create table ta( id int,name varchar(20))
    insert ta select 510, '计算机'
    insert ta select 511, '计算机'
    insert ta select 512, '计算机'
    insert ta select 512, '摄像机'
    insert ta select 512, '相机'
    insert ta select 513, 'U盘'
    insert ta select 514, 'U盘'
    insert ta select 515, '计算机'create function test(@id int)
    returns varchar(1000)
    as
    begin
    declare @s varchar(1000)
    select @s=isnull(@s+',','')+name from ta where id=@id
    return @s
    end查询:
    select distinct id,[name]=dbo.test(id) from taid          name
    ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    510         计算机
    511         计算机
    512         计算机,摄像机,相机
    513         U盘
    514         U盘
    515         计算机(6 行受影响)
      

  5.   

    不错,感谢各位的帮忙,看来想直接用SQL语句实现不容易啊。