麻烦大神看看怎么把这个查询结果分出来,用的是oracle

如图是sql的查询结果

select dep.depcode,dep.depname,tdh.shorgcode 门店编码,tdh.shorgname 配送门店,sum(tdb.pstotal) 配送金额  from  tDstPsbody tdb,tDstPshead tdh,tOrgDept dep 

where tdb.billno=tdh.billno and tdb.depid=dep.depid and tdh.rptdate='2022-10-14' and tdh.shorgcode in ('0030','0017') group by dep.depcode,dep.depname,tdh.shorgcode,tdh.shorgname 

union 

 select dep.depcode,dep.depname,tdh.thorgcode 门店编码,tdh.thorgname 配送门店,(-sum(tdb.thtotal)) 配送金额  from  tDstRtnbody tdb,tDstRtnhead tdh,tOrgDept dep 

where tdb.billno=tdh.billno and tdb.depid=dep.depid and tdh.rptdate='2022-10-14' and tdh.thorgcode in ('0030','0017') group by dep.depcode,dep.depname,tdh.thorgcode,tdh.thorgname

image.png

我想要的查询结果是:小于0的分到 配送退货金额,大于0的分到配送金额

大神看看 怎么能实现?

image.png

FineReport 15900334107 发布于 2022-10-16 16:39 (编辑于 2022-10-16 16:41)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共2回答
最佳回答
0
axingLv6专家互助
发布于2022-10-16 16:51

select dep.depcode,dep.depname,tdh.shorgcode 门店编码,tdh.shorgname 配送门店,

sum(case when tdb.pstotal>=0 then tdb.pstotal else 0 end) as  配送金额,

sum(case when tdb.pstotal<0 then tdb.pstotal else 0 end) as  配送退货金额

from  tDstPsbody tdb,tDstPshead tdh,tOrgDept dep 

where tdb.billno=tdh.billno and tdb.depid=dep.depid and tdh.rptdate='2022-10-14' and tdh.shorgcode in ('0030','0017') 

group by dep.depcode,dep.depname,tdh.shorgcode,tdh.shorgname 

最佳回答
0
蒲公英FZLJLv5初级互助
发布于2022-10-17 11:05

sql改写

select dep.depcode,dep.depname,tdh.shorgcode 门店编码,tdh.shorgname 配送门店,sum(tdb.pstotal) 配送金额,0 配送退货金额  from  tDstPsbody tdb,tDstPshead tdh,tOrgDept dep 

where tdb.billno=tdh.billno and tdb.depid=dep.depid and tdh.rptdate='2022-10-14' and tdh.shorgcode in ('0030','0017') group by dep.depcode,dep.depname,tdh.shorgcode,tdh.shorgname 

union 

 select dep.depcode,dep.depname,tdh.thorgcode 门店编码,tdh.thorgname 配送门店,0 配送金额,(-sum(tdb.thtotal)) 配送退货金额  from  tDstRtnbody tdb,tDstRtnhead tdh,tOrgDept dep 

where tdb.billno=tdh.billno and tdb.depid=dep.depid and tdh.rptdate='2022-10-14' and tdh.thorgcode in ('0030','0017') group by dep.depcode,dep.depname,tdh.thorgcode,tdh.thorgname

另一种可以在报表设计的时候使用过滤条件

image.png

  • 3关注人数
  • 297浏览人数
  • 最后回答于:2022-10-17 11:05
    请选择关闭问题的原因
    确定 取消
    返回顶部