create table A 
(
id int,
name varhcar(20),
amount decimal(16,3)

insert into A values (1,'人民币',100.00)
insert into A values (1,'人民币',100.00)
insert into A values (1,'美元',100.00)
insert into A values (1,'美元',100.00)
insert into A values (1,'人民币',100.00)
一张表A 三个字段 id ,name, amount
求根据id分组的amount的总值。
要求用函数写。
以这种格式显示结果。人民币:***, 美元***
求代码。

解决方案 »

  1.   

    /****************************************************************************************************************************************************** 
    合并分拆表数据 整理人:中国风(Roy) 日期:2008.06.06 
    ******************************************************************************************************************************************************/ --> --> (Roy)生成測試數據 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 
    go 
    Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab go 
      

  2.   

    create  table A  
    (
    id int,
    name varchar(20),
    amount decimal(16,3)
    )  
    insert into A values (1,'人民币',100.00)
    insert into A values (1,'人民币',100.00)
    insert into A values (1,'美元',100.00)
    insert into A values (1,'美元',100.00)
    insert into A values (1,'人民币',100.00)go
    create function f_str(@id int) returns varchar(1000)
    as 
    begin
    return(select '人民币:'+ltrim(sum(case when name='人民币' then amount else 0 end))+' 美元:'
                                   +ltrim(sum(case when name='美元'   then amount else 0 end))
                   from a group by id)    
    endgo
    select id,dbo.f_str(id) from a group by id
      

  3.   

    我想 要 根据id分, 然后再在相同的id里面分:人民币或美元的总和
      

  4.   

    4L的那个ID就是根据ID分的意思,你想根据ID=1分 就把1带给函数 想根据2分就把2带给函数
      

  5.   


    create  table A  
    (
    id int,
    name varchar(20),
    amount decimal(16,3)
    )  
    insert into A values (1,'人民币',100.00)
    insert into A values (1,'人民币',100.00)
    insert into A values (1,'美元',100.00)
    insert into A values (1,'美元',100.00)
    insert into A values (1,'人民币',100.00)
    insert into A values (2,'人民币',60.00)
    insert into A values (2,'美元',750.00)
    insert into A values (2,'美元',760.00)
    insert into A values (2,'人民币',640.00)create function GetTable() 
    returns 
    @T table 
    (id int,
     币种1 nvarchar(10),
     col1 decimal(16,3),
     币种2 nvarchar(10),
     col2 decimal(16,3))
    as 
    begin
       insert @T
       select id,'人民币:',sum(case name when '人民币' then amount else 0 end),
                 '美元:',sum(case name when '美元' then amount else 0 end) from A group by id
       return
    endselect * from dbo.GetTable()id          币种1        col1                                    币种2        col2
    ----------- ---------- --------------------------------------- ---------- ---------------------------------------
    1           人民币:       300.000                                 美元:        200.000
    2           人民币:       700.000                                 美元:        1510.000这样?