فهرست منبع

海盐订单账期出货查询总金额逻辑变更2022年1月12日 09:17:21

hu 4 سال پیش
والد
کامیت
fb597f4d63

+ 22 - 22
src/rest/openapi/restcontroller/wechatapp/orderform/SQL/订单出货未付款账期统计查询.sql

@@ -1,18 +1,18 @@
 declare @siteid varchar(20)
 declare @siteid varchar(20)
 set @siteid = $siteid$
 set @siteid = $siteid$
-select t1.fagentnum,fagentname,fagentshortname,pp_hr.displayname,isnull(t2.famount1,0) famount1,isnull(t3.famount2,0) famount2,isnull(t4.famount3,0) famount3,isnull(t5.famount4,0) famount4,isnull(t6.famount5,0) famount5,isnull(t7.famount6,0) ftotalamount from tagents t1 
-inner join 
+select t1.fagentnum,fagentname,fagentshortname,pp_hr.displayname,isnull(t2.famount1,0) famount1,isnull(t3.famount2,0) famount2,isnull(t4.famount3,0) famount3,isnull(t5.famount4,0) famount4,isnull(t6.famount5,0) famount5,(isnull(t2.famount1,0)+isnull(t3.famount2,0)+isnull(t4.famount3,0)+isnull(t5.famount4,0)+isnull(t6.famount5,0)) ftotalamount from tagents t1
+inner join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount6 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount6 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
 (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
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t7 on t1.fagentnum=t7.fagentnum and isnull(t7.famount6,0)>=0
 ) t7 on t1.fagentnum=t7.fagentnum and isnull(t7.famount6,0)>=0
@@ -20,14 +20,14 @@ left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount1 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount1 from (
 select  t1.fagentnum,t1.fsaler,sum(isnull(t4.famount,0)) famount from saorder t1
 select  t1.fagentnum,t1.fsaler,sum(isnull(t4.famount,0)) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 left join (
 left join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
 (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
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t2 on t7.fagentnum=t2.fagentnum and t7.fsaler=t2.fsaler and isnull(t2.famount1,0)>=0
 ) t2 on t7.fagentnum=t2.fagentnum and t7.fsaler=t2.fsaler and isnull(t2.famount1,0)>=0
@@ -35,14 +35,14 @@ left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount2 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount2 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
 (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
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t3 on t7.fagentnum=t3.fagentnum and t7.fsaler=t3.fsaler and isnull(t3.famount2,0)>=0
 ) t3 on t7.fagentnum=t3.fagentnum and t7.fsaler=t3.fsaler and isnull(t3.famount2,0)>=0
@@ -50,44 +50,44 @@ left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount3 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount3 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
 (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
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t4 on t7.fagentnum=t4.fagentnum and t7.fsaler=t4.fsaler and isnull(t4.famount3,0)>=0
 ) t4 on t7.fagentnum=t4.fagentnum and t7.fsaler=t4.fsaler and isnull(t4.famount3,0)>=0
-left join 
+left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount4 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount4 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid  and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
 (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
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t5 on t7.fagentnum=t5.fagentnum and t7.fsaler=t5.fsaler and isnull(t5.famount4,0)>=0
 ) t5 on t7.fagentnum=t5.fagentnum and t7.fsaler=t5.fsaler and isnull(t5.famount4,0)>=0
-left join 
+left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount5 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount5 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid  and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(fwriteoffamount,0) famount from saorder t1
 (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
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t6 on t7.fagentnum=t6.fagentnum and t7.fsaler=t6.fsaler and isnull(t6.famount5,0)>=0
 ) t6 on t7.fagentnum=t6.fagentnum and t7.fsaler=t6.fsaler and isnull(t6.famount5,0)>=0

+ 22 - 22
src/rest/openapi/restcontroller/wechatapp/orderform/SQL/订单出货未开票账期统计查询.sql

@@ -1,18 +1,18 @@
 declare @siteid varchar(20)
 declare @siteid varchar(20)
 set @siteid = $siteid$
 set @siteid = $siteid$
-select t1.fagentnum,fagentname,fagentshortname,pp_hr.displayname,isnull(t2.famount1,0) famount1,isnull(t3.famount2,0) famount2,isnull(t4.famount3,0) famount3,isnull(t5.famount4,0) famount4,isnull(t6.famount5,0) famount5,isnull(t7.famount6,0) ftotalamount from tagents t1 
-inner join 
+select t1.fagentnum,fagentname,fagentshortname,pp_hr.displayname,isnull(t2.famount1,0) famount1,isnull(t3.famount2,0) famount2,isnull(t4.famount3,0) famount3,isnull(t5.famount4,0) famount4,isnull(t6.famount5,0) famount5,(isnull(t2.famount1,0)+isnull(t3.famount2,0)+isnull(t4.famount3,0)+isnull(t5.famount4,0)+isnull(t6.famount5,0)) ftotalamount from tagents t1
+inner join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount6 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount6 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t7 on t1.fagentnum=t7.fagentnum and isnull(t7.famount6,0)>=0
 ) t7 on t1.fagentnum=t7.fagentnum and isnull(t7.famount6,0)>=0
@@ -20,14 +20,14 @@ left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount1 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount1 from (
 select  t1.fagentnum,t1.fsaler,sum(isnull(t4.famount,0)) famount from saorder t1
 select  t1.fagentnum,t1.fsaler,sum(isnull(t4.famount,0)) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120) group by t1.fagentnum,fsaler) t1
 left join (
 left join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,0,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t2 on t7.fagentnum=t2.fagentnum and t7.fsaler=t2.fsaler and isnull(t2.famount1,0)>=0
 ) t2 on t7.fagentnum=t2.fagentnum and t7.fsaler=t2.fsaler and isnull(t2.famount1,0)>=0
@@ -35,14 +35,14 @@ left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount2 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount2 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-1,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t3 on t7.fagentnum=t3.fagentnum and t7.fsaler=t3.fsaler and isnull(t3.famount2,0)>=0
 ) t3 on t7.fagentnum=t3.fagentnum and t7.fsaler=t3.fsaler and isnull(t3.famount2,0)>=0
@@ -50,44 +50,44 @@ left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount3 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount3 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-3,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t4 on t7.fagentnum=t4.fagentnum and t7.fsaler=t4.fsaler and isnull(t4.famount3,0)>=0
 ) t4 on t7.fagentnum=t4.fagentnum and t7.fsaler=t4.fsaler and isnull(t4.famount3,0)>=0
-left join 
+left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount4 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount4 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid  and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)>=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) and  CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-6,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t5 on t7.fagentnum=t5.fagentnum and t7.fsaler=t5.fsaler and isnull(t5.famount4,0)>=0
 ) t5 on t7.fagentnum=t5.fagentnum and t7.fsaler=t5.fsaler and isnull(t5.famount4,0)>=0
-left join 
+left join
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount5 from (
 (select t1.fagentnum,t1.fsaler,(t1.famount-t2.famount) famount5 from (
 select  t1.fagentnum,t1.fsaler,sum(t4.famount) famount from saorder t1
 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 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 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
 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 CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) group by t1.fagentnum,fsaler) t1
 where t1.fstatus in('审核','关闭') and t3.fstatus='关闭' and t1.siteid=@siteid  and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120) group by t1.fagentnum,fsaler) t1
 inner join (
 inner join (
-select fagentnum,sum(t.famount) famount,fsaler from 
+select fagentnum,sum(t.famount) famount,fsaler from
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 (select distinct t2.saorderdetailid,t1.fagentnum,t1.fsaler,isnull(finvoiceamount,0) famount from saorder t1
 inner join saorderdetail t2 on t1.siteid=t2.siteid and t1.saorderid=t2.fparentid
 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 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
 left join sainvoice t3 on t4.siteid=t3.siteid and t4.fparentid=t3.sainvoiceid
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 where t1.fstatus in('审核','关闭') and t1.siteid=@siteid and CONVERT(VARCHAR(10),t1.checkdate,120)<=CONVERT(VARCHAR(10),DATEADD(mm,-12,getdate()),120)) t group by fagentnum,FSALER) t2 on t1.fagentnum=t2.fagentnum and t1.fsaler=t2.fsaler
 ) t6 on t7.fagentnum=t6.fagentnum and t7.fsaler=t6.fsaler and isnull(t6.famount5,0)>=0
 ) t6 on t7.fagentnum=t6.fagentnum and t7.fsaler=t6.fsaler and isnull(t6.famount5,0)>=0