回答:with as base (select a.saleno ,sum(case when b.type=0 then a.disc else -a.disc end) sum_disc ,sum(case when b.type=0 then a.amt else -a.amt end) sum_amt from td_sale_detail a inner join td_sale b on a.companyno=b.companyno and a.shop=b.shop and a.saleno=b.salenoleft join tb_goods_lang C on A.companyno=C.companyno and A.pluno=C.pluno and C.lang_type='${para_lang_type}'left join tb_goods e on A.companyno=e.companyno and A.pluno=e.plunoinner join ta_org o on o.companyno=a.companyno and o.organizationno=a.shopleft join TD_SALE_DETAIL_AGIO f on f.saleno=a.saleno and a.ITEM=f.Mitem and a.COMPANYNO=f.COMPANYNO and a.shop=f.shopleft join ta_Reason_Lang g on f.bsno=g.bsno and g.companyno=f.companyno and g.lang_type='${para_lang_type}'left join TB_CATEGORY h on e.sno=h.category and h.companyno=e.companyno${if(rechargeGIFT==0,"left join tg_recharge f on f.rechargeno=b.ofno and f.companyno=b.companyno","")}where a.companyno='${para_companyno}' and -- o.BELFIRM='${para_organizationno}' and b.bdate between '${format(para_bdate1,"yyyyMMdd")}' and '${format(para_bdate2,"yyyyMMdd")}' ${if(len(para_organizationno_shop)==0,""," and a.shop in ('" + para_organizationno_shop + "')")} ${if(len(para_type)==0,""," and b.type in ('" + para_type + "')")} ${if(len(para_pluno)==0,""," and a.pluno in ('" + para_pluno+ "')")} ${if(len(para_sno)==0,""," and e.sno in ('" + JOINARRAY(para_sno,"','")+ "')")} ${if(len(para_plu_name)==0,""," and c.plu_name like '%%" + para_plu_name+ "%%'")} ${if(len(para_saleno)==0,""," and a.saleno in ('" + para_saleno+ "')")} ${switch(para_TCType,0,"and (a.IsPackage ='N' or a.IsPackage is null)",1,"and a.PackageMaster='N' and a.IsPackage='Y'",2,"and a.PackageMaster='Y'",3,"and (a.PackageMaster='N' or a.PackageMaster is null)",4,"and (a.PackageMaster='Y' or a.IsPackage='N')")} and b.type!=16 ${if(len(ORDERCHANNEL)==0,"","and ORDERCHANNEL in ('"+ORDERCHANNEL+"')")} ${if(dzx==0,"and b.order_id is not null","")} ${if(len(gift)==0,"","and ISGIFT = 'Y'")} and (a.dishesstatus not in ('3','4') or a.dishesstatus is null) ${if(len(para_ofno)==0,""," and b.ofno = '" + para_ofno+ "'")}${if(rechargeGIFT==0,"and f.rechargeno is not null","")}group by a.saleno order by b.bdate,b.Machine,b.SquadNO,a.saleno,a.item), as base1 (select a.saleno,b.bdate,b.Machine,b.CardNO,b.SquadNO,b.opno, case when b.otype =3 then 3 when b.otype=4 then 4 else b.type end type, a.companyno,a.shop, a.item, a.clerkno,a.accno, a.pluno, a.featureno, a.plubarcode, a.scanno, a.unit,a.oldprice, a.price, case when b.type=0 then a.qty else -a.qty end qty ,case when b.type=0 then a.uamt else -a.uamt end uamt ,case when b.type=0 then a.qty else -a.qty end packqty ,case when b.type=0 then a.disc else -a.disc end packdisc ,case when b.type=0 then a.amt else -a.amt end packamt ,case when b.type=0 then a.uamt else -a.uamt end packuamt , a.counteramt, a.point_qty, b.ofno,a.oitem, a.sdate, a.stime, a.cnfflg,C.plu_name ,case when a.ISGIFT='Y' then '是' else '否' end isgift ,g.REASON_NAME GIFTREASON ,b.ORDERCHANNEL ,e.sno ,h.up_categoryfrom td_sale_detail a inner join td_sale b on a.companyno=b.companyno and a.shop=b.shop and a.saleno=b.salenoleft join tb_goods_lang C on A.companyno=C.companyno and A.pluno=C.pluno and C.lang_type='${para_lang_type}'left join tb_goods e on A.companyno=e.companyno and A.pluno=e.plunoinner join ta_org o on o.companyno=a.companyno and o.organizationno=a.shopleft join TD_SALE_DETAIL_AGIO f on f.saleno=a.saleno and a.ITEM=f.Mitem and a.COMPANYNO=f.COMPANYNO and a.shop=f.shopleft join ta_Reason_Lang g on f.bsno=g.bsno and g.companyno=f.companyno and g.lang_type='${para_lang_type}'left join TB_CATEGORY h on e.sno=h.category and h.companyno=e.companyno${if(rechargeGIFT==0,"left join tg_recharge f on f.rechargeno=b.ofno and f.companyno=b.companyno","")}where a.companyno='${para_companyno}' and -- o.BELFIRM='${para_organizationno}' and b.bdate between '${format(para_bdate1,"yyyyMMdd")}' and '${format(para_bdate2,"yyyyMMdd")}' ${if(len(para_organizationno_shop)==0,""," and a.shop in ('" + para_organizationno_shop + "')")} ${if(len(para_type)==0,""," and b.type in ('" + para_type + "')")} ${if(len(para_pluno)==0,""," and a.pluno in ('" + para_pluno+ "')")} ${if(len(para_sno)==0,""," and e.sno in ('" + JOINARRAY(para_sno,"','")+ "')")} ${if(len(para_plu_name)==0,""," and c.plu_name like '%%" + para_plu_name+ "%%'")} ${if(len(para_saleno)==0,""," and a.saleno in ('" + para_saleno+ "')")} ${switch(para_TCType,0,"and (a.IsPackage ='N' or a.IsPackage is null)",1,"and a.PackageMaster='N' and a.IsPackage='Y'",2,"and a.PackageMaster='Y'",3,"and (a.PackageMaster='N' or a.PackageMaster is null)",4,"and (a.PackageMaster='Y' or a.IsPackage='N')")} and b.type!=16 ${if(len(ORDERCHANNEL)==0,"","and ORDERCHANNEL in ('"+ORDERCHANNEL+"')")} ${if(dzx==0,"and b.order_id is not null","")} ${if(len(gift)==0,"","and ISGIFT = 'Y'")} and (a.dishesstatus not in ('3','4') or a.dishesstatus is null) ${if(len(para_ofno)==0,""," and b.ofno = '" + para_ofno+ "'")}${if(rechargeGIFT==0,"and f.rechargeno is not null","")} order by b.bdate,b.Machine,b.SquadNO,a.saleno,a.item)select * from base t1left join base1 t2on t1.saleno=t2.saleno试一下能成功吗,我的思路是第一步根据销售订单分组求和得出每个销售订单对应的累计金额,分组后销售订单是唯一的,第二步根据你的where条件查出其他展示列,通过唯一的销售订单进行关联形成汇总表,这个思路要求base和base1一一对应开窗函数写法:(可能有重复数据)select b.bdate,b.Machine,b.CardNO,b.SquadNO,b.opno, case when b.otype =3 then 3 when b.otype=4 then 4 else b.type end type, a.companyno,a.shop, a.saleno, a.item, a.clerkno,a.accno, a.pluno, a.featureno, a.plubarcode, a.scanno, a.unit,a.oldprice, a.price, case when b.type=0 then a.qty else -a.qty end qty ,sum(case when b.type=0 then a.disc else -a.disc end) over (partition by a.saleno) as sum_disc ,sum(case when b.type=0 then a.amt else -a.amt end ) over (partition by a.saleno) as sum_amt ,case when b.type=0 then a.uamt else -a.uamt end uamt ,case when b.type=0 then a.qty else -a.qty end packqty ,case when b.type=0 then a.disc else -a.disc end packdisc ,case when b.type=0 then a.amt else -a.amt end packamt ,case when b.type=0 then a.uamt else -a.uamt end packuamt , a.counteramt, a.point_qty, b.ofno,a.oitem, a.sdate, a.stime, a.cnfflg,C.plu_name ,case when a.ISGIFT='Y' then '是' else '否' end isgift ,g.REASON_NAME GIFTREASON ,b.ORDERCHANNEL ,e.sno ,h.up_categoryfrom td_sale_detail a inner join td_sale b on a.companyno=b.companyno and a.shop=b.shop and a.saleno=b.salenoleft join tb_goods_lang C on A.companyno=C.companyno and A.pluno=C.pluno and C.lang_type='${para_lang_type}'left join tb_goods e on A.companyno=e.companyno and A.pluno=e.plunoinner join ta_org o on o.companyno=a.companyno and o.organizationno=a.shopleft join TD_SALE_DETAIL_AGIO f on f.saleno=a.saleno and a.ITEM=f.Mitem and a.COMPANYNO=f.COMPANYNO and a.shop=f.shopleft join ta_Reason_Lang g on f.bsno=g.bsno and g.companyno=f.companyno and g.lang_type='${para_lang_type}'left join TB_CATEGORY h on e.sno=h.category and h.companyno=e.companyno${if(rechargeGIFT==0,"left join tg_recharge f on f.rechargeno=b.ofno and f.companyno=b.companyno","")}where a.companyno='${para_companyno}' and -- o.BELFIRM='${para_organizationno}' and b.bdate between '${format(para_bdate1,"yyyyMMdd")}' and '${format(para_bdate2,"yyyyMMdd")}' ${if(len(para_organizationno_shop)==0,""," and a.shop in ('" + para_organizationno_shop + "')")} ${if(len(para_type)==0,""," and b.type in ('" + para_type + "')")} ${if(len(para_pluno)==0,""," and a.pluno in ('" + para_pluno+ "')")} ${if(len(para_sno)==0,""," and e.sno in ('" + JOINARRAY(para_sno,"','")+ "')")} ${if(len(para_plu_name)==0,""," and c.plu_name like '%%" + para_plu_name+ "%%'")} ${if(len(para_saleno)==0,""," and a.saleno in ('" + para_saleno+ "')")} ${switch(para_TCType,0,"and (a.IsPackage ='N' or a.IsPackage is null)",1,"and a.PackageMaster='N' and a.IsPackage='Y'",2,"and a.PackageMaster='Y'",3,"and (a.PackageMaster='N' or a.PackageMaster is null)",4,"and (a.PackageMaster='Y' or a.IsPackage='N')")} and b.type!=16 ${if(len(ORDERCHANNEL)==0,"","and ORDERCHANNEL in ('"+ORDERCHANNEL+"')")} ${if(dzx==0,"and b.order_id is not null","")} ${if(len(gift)==0,"","and ISGIFT = 'Y'")} and (a.dishesstatus not in ('3','4') or a.dishesstatus is null) ${if(len(para_ofno)==0,""," and b.ofno = '" + para_ofno+ "'")}${if(rechargeGIFT==0,"and f.rechargeno is not null","")} order by b.bdate,b.Machine,b.SquadNO,a.saleno,a.item