table 1 c_receipt_detailfleld:
  
   VZPSJ                       NOT NULL VARCHAR2(40)
   NSFXM_ID                    NOT NULL NUMBER(6)
   FEE                         NOT NULL NUMBER(16,6)
   
  内容:   VZPSJ                                      NSFXM_ID        FEE   040420-040000796                                 16          0
   040420-040000797                                  1       2.25
   040420-040000797                                 16        .05
   040420-010001554                                  1 634.200012
   040420-010001554                                 16   -.000012
   040420-010001555                                  1       28.5
   040420-010001555                                 16          0
   040420-010001556                                  1       11.4
   040420-010001556                                 16          0
   040420-010001557                                  1  17.099998
   040420-010001557                                 16    .000002
   040420-010001558                                  1  38.899998
   040420-010001558                                  3    .000002
   040420-010001559                                  1 682.200012
   040420-010001559                                 16   -.000012table 2 c_charge_item  fleld:    NSFXM_ID                    NOT NULL NUMBER(5)
    VSFXM_MC                    NOT NULL VARCHAR2(30)
    VZJF_PY                     NOT NULL VARCHAR2(10)
    VZJF_WB                     NOT NULL VARCHAR2(10)  内容:    NSFXM_ID VSFXM_MC                       VZJF_PY    VZJF_WB       1     西药费                         xyf        sax
       2     中成药                         zcy        kda
       3     中草药                         zcy        kaa
       4     检查费                         jcf        ssx
       5     CT费                           CTf        CTx
       6     核磁费                         hcf        sdx
       7     B超                            Bc         Bf
       8     输氧费                         syf        lrx
       9     手术费                         ssf        rsx
      10     治疗费                         zlf        iux
      11     放射费                         fsf        ytx
      12     化验费                         hyf        wcx
      13     输血费                         sxf        ltx
      14     挂号费                         ghf        rkx
      15     其它                           qt         ap
      16     四舍五入值                     sswrz      lwgtw
      17     一次性用品                     ycxyp      gunek     怎样通过一个语句查出类似于这样
  
     select vzpjs,sum(西药费),sum(中成药),sum(中草药)...
     from c_receipt_detail a,c_charge_item b
     where a.nsfxm_id = b.nsfxm_id
     group by vzpjs;     而 c_charge_item 添加字段后不需要修改sql

解决方案 »

  1.   

    通过查询生成 长字符串
    select distinct vzpjs,(select sum(fee) from c_receipt_detail b where b.nsfxm_id = tmp_id),..... from c_receipt_detail a
    用动态SQL
    ......
    与(select sum(fee) from c_receipt_detail b where b.nsfxm_id = tmp_id)相似其中tmp_id是c_charge_item 中各行的值
      

  2.   

    不太明白
    我是想要这样的结果
     select vzpjs,sum(decode(nsfxm_id,1,fee,0)) as “西药费“,
                  sum(decode(nsfxm_id,2,fee,0)) as “中药费“,
                  sum(decode(nsfxm_id,3,fee,0)) as “草药费“,
                        ...
     from c_charge_item,c_receipt_detail
     where  而当c_charge_item 添加新的汇总项目时补需要修改SQL