這不是分組求和嗎?select id,pay=sum(pay) from tb group by id

解决方案 »

  1.   

    Pay字段是字符型的,不能求和的。
      

  2.   

    create table tb(id int,pay varchar(20))
    insert into tb
    select    1  ,   '1000(CNY)'
    union select      1   ,  '1000(HKD'
    union select      2   ,  '1000(CNY)'insert into tb select      2   ,  '1000(hkd)'
    insert into tb select      3   ,  '1000(hkd)'select * from tbselect id,max(m.c)
    from (select a.id,a.pay+'+'+b.pay as c
    from tb a,tb b
    where a.id=b.id and a.pay<>b.pay ) m
    group by id 
    union 
    select id,max(pay)
    from tb
    group by id
    having count(*)=1
      

  3.   

    create function f1(@id int)
    returns varchar(100)
    as
    begin
    declare @re varchar(1000)
    set @re=''
    select @re=@re+'+'+pay from t1 where id=@id
    return stuff(@re,1,1,'')
    end
    go
    select id,dbo.f1(id) from t1
    group by id
      

  4.   

    create table t1(id int,pay varchar(20))
    insert into t1
    select    1  ,   '1000(CNY)'
    union select      1   ,  '1000(HKD)'
    union select      2   ,  '1000(CNY)'
    insert into t1 select      2   ,  '1000(hkd)'
    insert into t1 select      3   ,  '1000(hkd)'
    go
    create function f1(@id int)
    returns varchar(1000)
    as
    begin
    declare @re varchar(1000)
    set @re=''
    select @re=@re+'+'+pay from t1 where id=@id
    return stuff(@re,1,1,'')
    end
    go
    select id,dbo.f1(id) from t1
    group by iddrop table t1
    drop function dbo.f1
      

  5.   

    --or
    create table t1(id int,pay varchar(20))
    insert into t1
    select    1  ,   '1000(CNY)'
    union select      1   ,  '1000(HKD)'
    union select      2   ,  '1000(CNY)'
    insert into t1 select      2   ,  '1000(hkd)'
    insert into t1 select      3   ,  '1000(hkd)'
    goselect id,min(pay)+case count(1) when 1 then '' else '+'+max(pay) end from t1
    group by iddrop table t1
      

  6.   


    --测试数据
    create table tb(id int,pay varchar(20))
    insert into tb
    select    1  ,   '1000(CNY)'
    union select      1   ,  '1000(HKD'
    union select      2   ,  '1000(CNY)'
    insert into tb select      2   ,  '1000(hkd)'
    insert into tb select      3   ,  '1000(hkd)'
    go
    --建函数
    create function f_h(@id int)
    returns varchar(1000)
    as 
    begin
       declare @char varchar(1000)
       set @char=''
       select @char=@char+'+'+pay from tb where id=@id
       return right(@char,len(@char)-1)
    end
    go
    --查询
    select id,dbo.f_h(id) as pay from tb group by id
    --删除测试数据
    drop table tb
    drop function f_h
    /*结果
    id          pay                     
    ----------- ---------------------
    1           1000(CNY)+1000(HKD
    2           1000(CNY)+1000(hkd)
    3           1000(hkd)(所影响的行数为 3 行)*/
      

  7.   

    其实可建一存储过程,无论表1有多少记录,也不管同一id的记录有多少,均可返回你所需要的数据集
    Create   Procedure testAS---生成结果集
       If Object_id('tempdb..#Result') is not null
              Drop Table #Result
       Create table #Result
       (
          [ID]  nvarchar(10),
          pay  nvarchar(50)
       
       )
       
       insert into #Result
           select  id,'' as pay from table1 group by id---对结果集做循环,将要处理的数据集中的数据一一更新到结果集中
       Declare @id  nvarchar(10),
               @pay  nvarchar(50)
     
       Declare D_Loop Cursor For
            Select id,pay from table1 
       Open D_Loop
       FETCH NEXT FROM D_Loop INTO @id,@pay
       While (@@FETCH_STATUS=0)   Begin     Update #result Set pay=pay+'+'+@pay Where id=@id     FETCH NEXT FROM D_Loop INTO @id,@pay
       End
       ----释放游标
       Close D_Loop
       Deallocate D_Loop---去掉字段pay的每一个字符'+'
       Update #result set pay=substring(pay,2,len(pay)-1)
       
    ---显示结果集
      Select * from #result order by id--exec test