订单出货未付款金额查询.sql 1.2 KB

1234567891011121314
  1. select isnull(SUM(famount6),0) famount from (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount6 from (
  2. select t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
  3. inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
  4. inner join sainvoicedetail t4 on t4.fsourceid=t2.saorderdetailid and t4.siteid=t2.siteid
  5. inner join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
  6. where t1.fstatus in('ÉóºË','¹Ø±Õ') and t3.fstatus='¹Ø±Õ' and t1.siteid=$siteid$ and $where$ group by t1.fagentnum,fsaler) t1
  7. inner join (
  8. select fagentnum,sum(t.famount) famount,fsaler from
  9. (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
  10. inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
  11. left join sainvoicedetail t4 on t4.fsourceid=t2.saorderdetailid and t4.siteid=t2.siteid
  12. left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
  13. where t1.fstatus in('ÉóºË','¹Ø±Õ') and t1.siteid=$siteid$ and $where$) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
  14. ) t7 where isnull(t7.famount6,0)>=0