| 1234567891011121314 |
- select isnull(SUM(famount6),0) famount from (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount6 from (
- select t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
- inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
- inner join sainvoicedetail t4 on t4.fsourceid=t2.saorderdetailid and t4.siteid=t2.siteid
- inner join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
- where t1.fstatus in('ÉóºË','¹Ø±Õ') and t3.fstatus='¹Ø±Õ' and t1.siteid=$siteid$ and $where$ group by t1.fagentnum,fsaler) t1
- inner join (
- select fagentnum,sum(t.famount) famount,fsaler from
- (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
- inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
- left join sainvoicedetail t4 on t4.fsourceid=t2.saorderdetailid and t4.siteid=t2.siteid
- left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
- 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
- ) t7 where isnull(t7.famount6,0)>=0
|