This document contains a SQL query that selects and aggregates customer invoice and collection document data. It selects fields like customer code and name, payment terms, credit limit, document totals, dates, currencies, and aging buckets for outstanding balances. The query unions invoice and collection document data and orders by customer and date.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
365 views
Antiguedad de Saldos Query
This document contains a SQL query that selects and aggregates customer invoice and collection document data. It selects fields like customer code and name, payment terms, credit limit, document totals, dates, currencies, and aging buckets for outstanding balances. The query unions invoice and collection document data and orders by customer and date.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
DECLARE @LOCCUR VARCHAR(3)
SET @LOCCUR = '$'
SELECT T0.CardCode AS 'Cod S/N', T0.CardName AS Nombre, T1.[CardFName], T2.PYMNTGROUP, T1.CREDITLINE , 'TotalDoc'= (case when DocCur='USD' then (T0.DocTotalFC) else (DocTotal) end) , T0.DocNum AS 'N/Doc' , DOCDATE as 'Fech/Crea',GETDATE() as FechaInforme, T0.NUMATCARD as Referencia, T0.DOCDUEDATE as 'Fech/Venc.', DATEDIFF(day, docduedate, getdate()) as Das, 'Saldo Pendiente'= (case when DocCur='USD' then (T0.DocTotalFC-T0.PaidFC) else (DocTotal-PaidToDate) end) , DocCur as Moneda ,'Corriente' = (case when (Doccur=@LOCCUR and getdate() <= DocDueDate) then (DocTotal-PaidToDate) else (case when getdate() <= DocDueDate and Doccur='USD' then (DocTotalFC-PaidFC) else 0 end) end) , ' 0-30' = (case when (T0.Doccur=@LOCCUR and DATEDIFF(day, T0.docduedate, getdate())BETWEEN 0 and 30) then (T0.DocTotal-T0.PaidToDate) else (case when DATEDIFF(day, T0.docduedate, getdate())BETWEEN 0 AND 30 and T0.Doccur='USD' then (T0.DocTotalFC-T0.PaidFC) else 0 end) end) , ' 31-60' = (case when (DocCur=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=31 and DATEDIFF(day, docduedate, getdate())<=60) then (T0.DocTotalT0.PaidToDate) else (case when DATEDIFF(day, docduedate, getdate())>=31 and DATEDIFF(day, docduedate , getdate())<=60 and DocCur='USD' then (DocTotalFCPaidFc)else 0 end) end) , ' 61-90' = (case when (DocCur=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=61 and DATEDIFF(day, docduedate, getdate())<=90) then (T0.DocTotalT0.PaidToDate) else (case when DATEDIFF(day, docduedate, getdate())>=61 and DATEDIFF(day, docduedate, getdate())<=90 and DocCur='USD' then (DocTotalFCPaidFC) else 0 end) end), '90+' = (case when (DocCur=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=90) then (T0.DocTotal-T0.PaidToDate) else (case when (DocCur='USD' and DATEDIFF(day, docduedate, getdate())>=90) then (T0.DocTotalFC-T0.PaidFC) else 0 end) end), 'Factura' as 'Docto' FROM OINV T0 INNER JOIN OCRD T1 ON T0.CARDCODE= T1.CARDCODE INNER JOIN OCTG T2 ON T1.GROUPNUM = T2.GROUPNUM WHERE (T0.DocTotal-T0.PaidToDate > 0 or DocTotalFC-PaidFC > 0) union all SELECT T0.CardCode AS Cliente, T0.CardName AS Nombre, T1.[CardFName], T2.PYMNTGROUP, T1.CREDITLINE , 'TotalDoc'= (case when DocCurr='USD' then (T0.DocTotalFC) else (DocTotal) end) , T0.DocNum AS NoDocto, DOCDATE as FechaDocto,GETDATE() as FechaInforme, T0.CounterRef as Referencia, T0.DOCDUEDATE as Vencimiento, DATEDIFF(day, docduedate, getdate()) as DasVencimiento,
'Saldo'= (case when DocCurr='USD' then (T0.NoDocSumFC * -1) else
(T0.NoDocSum * -1) end) , DocCurr as Moneda , 'Por Vencer' = (case when (DocCurr=@LOCCUR and getdate() <= DocDueDate) then (T0.NoDocSum) else (case when getdate() <= DocDueDate and DocCurr='USD' then (T0.NoDocSumFC * -1) else 0 end) end) , ' 0-30' = (case when (DocCurr=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=0 and DATEDIFF (day, docduedate, getdate())<=30) then (T0.NoDocSum * -1) else (case when DATEDIFF(day, docduedate, getdate())>=0 and DATEDIFF(day, docduedate, getdate())<=30 and DocCurr='USD' then (T0.NoDocSumFC * -1) else 0 end) end) , ' 31-60' = (case when (DocCurr=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=31 and DATEDIFF(day, docduedate, getdate())<=60) then (T0.NoDocSum * -1) else (case when DATEDIFF(day, docduedate, getdate())>=31 and DATEDIFF(day, docduedate , getdate())<=60 and DocCurr='USD' then (T0.NoDocSumFC * -1)else 0 end) end) , ' 61-90' = (case when (DocCurr=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=61 and DATEDIFF(day, docduedate, getdate())<=90) then (T0.NoDocSum * -1) else (case when DATEDIFF(day, docduedate, getdate())>=61 and DATEDIFF(day, docduedate, getdate())<=90 and DocCurr='USD' then (T0.NoDocSumFC * -1) else 0 end) end), '90+' = (case when (DocCurr=@LOCCUR and DATEDIFF(day, docduedate, getdate())>=90) then (T0.NoDocSum * -1) else (case when (DocCurr='USD' and DATEDIFF(day, docduedate, getdate())>=90) then (T0.NoDocSumFC * -1) else 0 end) end), 'Cobranza' as 'Docto' FROM ORCT T0 INNER JOIN OCRD T1 ON T0.CARDCODE= T1.CARDCODE INNER JOIN OCTG T2 ON T1.GROUPNUM = T2.GROUPNUM WHERE (T0.NoDocSum > 0 or T0.NoDocSumFC > 0) AND T0.[DocType] = 'C' AND T0.OpenBal > 0 AND T0.[JrnlMemo] not like '%%Cancelado%%' ORDER BY T0.CardCode, T0.DocDate
Cooriente = (
case when (Doccur=@LOCCUR and getdate() <= DocDueDate) then (DocTotal-PaidToDate) else ( case when getdate() <= DocDueDate and Doccur='USD' then (DocTotalFC-PaidFC) else 0 end) end)