原表如下:
品名-----币种----金额
A-------RMB -----100
A-------USD------200
A-------USD------300
B-------RMB------400
B-------USD------50
B-------RMB------30
想得到如下结果:
品名----币种-----金额---------备注
A-------RMB -----100-------A下含有美金:500元
A-------USD------200-------A下含有美金:500元
A-------USD------300-------A下含有美金:500元
B-------RMB------400------ B下含有美金:50元
B-------USD------50------- B下含有美金:50元
B-------RMB------30------- B下含有美金:50元
就是查询结果多个备注,相同品名下的美金合计然后放到备注字段里表达出来    

解决方案 »

  1.   

    select t.*,
           品名+'下含有美金:'+ltrim((select sum(金额) from tb where 品名=t.品名 and 币种='USD'))+'元' 备注from tb t
      

  2.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([品名] nvarchar(1),[币种] nvarchar(3),[金额] int)
    Insert tb
    select N'A',N'RMB',100 union all
    select N'A',N'USD',200 union all
    select N'A',N'USD',300 union all
    select N'B',N'RMB',400 union all
    select N'B',N'USD',50 union all
    select N'B',N'RMB',30
    Go
    select t.*,
           品名+'下含有美金:'+ltrim((select sum(金额) from tb where 品名=t.品名 and 币种='USD'))+'元' 备注
    from tb t
    /*
    品名   币种   金额          备注
    ---- ---- ----------- ---------------------------
    A    RMB  100         A下含有美金:500元
    A    USD  200         A下含有美金:500元
    A    USD  300         A下含有美金:500元
    B    RMB  400         B下含有美金:50元
    B    USD  50          B下含有美金:50元
    B    RMB  30          B下含有美金:50元(6 row(s) affected)
    */
      

  3.   

    非常谢谢,本人是新手,我知道t是个临时的表,你上述代码我运行的确是没有问题,可我就是不知道这个t怎么个理解。因为我发的那个原表本来就是5个表union连接起来的视图查询后的结果。我就是不知道在这个视图里面怎么加上你的那个t 谢谢指导,不才。
      

  4.   

    select *,
           品名+'下含有美金:'+ltrim((select sum(金额) from (把临时表括起来)a where 品名=t.品名 and 币种='USD'))+'元' 备注
    from (把临时表括起来) t如果直接是视图的话
    select *,
           品名+'下含有美金:'+ltrim((select sum(金额) from 视图名 where 品名=t.品名 and 币种='USD'))+'元' 备注
    from 视图名 t
      

  5.   

    如果不是SQL2000,05及以上的话可以
    ;with cte as
    (
    ... --你的union语句
    )select *,
           品名+'下含有美金:'+ltrim((select sum(金额) from cte where 品名=t.品名 and 币种='USD'))+'元' 备注
    from cte t
      

  6.   

    if not object_id('tb') is null
        drop table tb
    Go
    Create table tb([品名] nvarchar(1),[币种] nvarchar(3),[金额] int)
    Insert tb
    select N'A',N'RMB',100 union all
    select N'A',N'USD',200 union all
    select N'A',N'USD',300 union all
    select N'B',N'RMB',400 union all
    select N'B',N'USD',50 union all
    select N'B',N'RMB',30
    Go
    --想得到如下结果:
    --品名----币种-----金额---------备注
    --A-------RMB -----100-------A下含有美金:500元
    --A-------USD------200-------A下含有美金:500元
    --A-------USD------300-------A下含有美金:500元
    --B-------RMB------400------ B下含有美金:50元
    --B-------USD------50------- B下含有美金:50元
    --B-------RMB------30------- B下含有美金:50元select *,品名,+'下含有美金:'+ltrim((select sum(金额) from tb where 币种='USD' and t.品名=品名))+'元' as 备注 from tb t
     
     品名   币种   金额          品名   备注
    ---- ---- ----------- ---- --------------------------
    A    RMB  100         A    下含有美金:500元
    A    USD  200         A    下含有美金:500元
    A    USD  300         A    下含有美金:500元
    B    RMB  400         B    下含有美金:50元
    B    USD  50          B    下含有美金:50元
    B    RMB  30          B    下含有美金:50元(6 行受影响)
      

  7.   

    非常谢谢 ,本人用的是sql2000 根据4楼的提示,我可以结合视图查询 成功了,不过因为视图本身不包括这个备注,所以我要再把视图进行查询,能不能直接视图查询出来就包括了呢。视图一部分如下:
    create view fwkpzb
    as 
        select  
           hyck.idx,业务编号,提单号,关单号,开航日期,船名航次,ISNULL((SELECT dxzl.中文名 from dxzl where idx=hyck.客户_DXZL_中文名),'') as 客户,
           '业务编号:'+Rtrim(业务编号)+CHAR(10)+'提单号:'+Rtrim(关单号)+CHAR(10)+'此发票共含美金:'  as 发票备注,
           ISNULL(fysr_sr.发票号,'') as 发票号,ISNULL(fysr_sr.币种,'') as 币种,ISNULL(fysr_sr.PC,'') as PC,
           ISNULL((SELECT 中文名 FROM dxzl WHERE idx = fysr_sr.应收对象),'') AS 应收对象,str(fysr_sr.idx,10)+'fysr' as midx,
           ISNULL((select 中文名 from fymx where idx=费用明细_fymx),'') as 费用明细,数量,round(fysr_sr.单价*fysr_sr.汇率,2)as 单价,round(数量*fysr_sr.单价*fysr_sr.汇率,2)as 总价,round(数量*fysr_sr.单价,2)as 原总价,
           fysr_sr.汇率,fysr_sr.开票抬头,fysr_sr.备注
        from hyck left join fysr_sr on hyck.idx=fysr_sr.hyck_idx
        where fysr_sr.pc='PP'上面视图的第7行,此发票共含有美金: 我想写在这个后面。业务编号可以理解为品名,原总价就是每个品名下不同币种的价格。
    价格和品名都在fysr_sr这个表里面,业务编号在hyck这个表里面。