Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8451

Cumulative Sales SQL Query

$
0
0

Well I have the following Cumulative Sales SQL query that used to run very well in the last month of May. But this month, I don't know what got into it, it does not properly order the transactions by either date or doc number and the invoices are not appearing first then credit notes last to give the right flow of transactions throughout the given month.

 

SELECT  T0.RefDate as 'Date',T0.BaseRef as 'Document #',(SELECT   CASE WHEN T0.TransType > '13' THEN 'Credit Note' ELSE 'Invoice' END) AS 'Document Type',
CASE WHEN T0.TransType = '13' THEN SUBSTRING(T2.CardName,1,27) ELSE SUBSTRING(T3.CardName,1,27) END AS 'Customer Name' ,
CASE WHEN T0.TransType = '13' THEN (ISNULL(T0.FcTotal,0) - ISNULL(T2.VatSumFC,0)) ELSE (ISNULL(T0.FcTotal,0) + ISNULL(T3.VatSumFC,0)) END AS 'Exclusive VAT AMount',
(SELECT   CASE WHEN T5.TransType = '13' THEN SUM(ISNULL(T5.FcTotal,0) - ISNULL(T6.VatSumFC,0)) ELSE SUM(ISNULL(T5.FcTotal,0) + ISNULL(T7.VatSumFC,0)) END
FROM SAP_LOG_TZN_PROD.dbo.OJDT T5
LEFT JOIN SAP_LOG_TZN_PROD.dbo.OINV T6 ON T6.DocNum = T5.BaseRef AND T5.TransType = T6.ObjType
LEFT JOIN SAP_LOG_TZN_PROD.dbo.ORIN T7 ON T7.DocNum = T5.BaseRef AND T5.TransType = T7.ObjType
WHERE T0.TransId >= T5.TransId AND T5.TransType = T0.TransType AND T5.TransType IN ('13','14') AND MONTH(T5.RefDate) = MONTH(GetDate()) AND YEAR(T5.RefDate) = YEAR(GetDate())
GROUP BY T5.TransType
) as 'Running Total'
FROM SAP_LOG_TZN_PROD.dbo.OJDT T0
INNER JOIN SAP_LOG_TZN_PROD.dbo.JDT1 T4 ON T4.TransId = T0.TransId
LEFT JOIN SAP_LOG_TZN_PROD.dbo.OINV T2 ON T2.DocNum = T0.BaseRef AND T0.TransType = T2.ObjType
LEFT JOIN SAP_LOG_TZN_PROD.dbo.ORIN T3 ON T3.DocNum = T0.BaseRef AND T0.TransType = T3.ObjType
WHERE T0.TransType IN ('13','14') AND MONTH(T0.RefDate) = MONTH(GetDate()) AND YEAR(T0.RefDate) = YEAR(GetDate())
GROUP BY T0.RefDate,T3.DocNum,T2.DocNum,  T0.TransType, T0.FcTotal, T2.VatSumFC, T3.VatSumFC, T0.TransId,  T0.TransType,T0.BaseRef, T2.CardName, T3.CardName
UNION ALL
SELECT DISTINCT  GETDATE()  ,'N/A','TOTAL' ,'GRAND TOTAL INCLUDING CREDIT NOTES',  0 ,
(SELECT  SUM(ISNULL(T5.FcTotal,0) - ISNULL(T6.VatSumFC,0)  + ISNULL(T7.VatSumFC,0))
FROM SAP_LOG_TZN_PROD.dbo.OJDT T5
LEFT JOIN SAP_LOG_TZN_PROD.dbo.OINV T6 ON T6.DocNum = T5.BaseRef AND T5.TransType = T6.ObjType
LEFT JOIN SAP_LOG_TZN_PROD.dbo.ORIN T7 ON T7.DocNum = T5.BaseRef AND T5.TransType = T7.ObjType
WHERE  T5.TransType IN ('13','14')  AND MONTH(T5.RefDate) = MONTH(GetDate()) AND YEAR(T5.RefDate) = YEAR(GetDate()))  as 'Running Total'
FROM SAP_LOG_TZN_PROD.dbo.OJDT T0
LEFT JOIN SAP_LOG_TZN_PROD.dbo.OINV T2 ON T2.DocNum = T0.BaseRef AND T0.TransType = T2.ObjType
LEFT JOIN SAP_LOG_TZN_PROD.dbo.ORIN T3 ON T3.DocNum = T0.BaseRef AND T0.TransType = T3.ObjType
WHERE MONTH(T0.RefDate) = MONTH(GetDate()) AND YEAR(T0.RefDate) = YEAR(GetDate())
GROUP BY T0.RefDate,T3.DocNum,T2.DocNum,   T0.FcTotal, T2.VatSumFC, T3.VatSumFC, T0.TransId,  T0.TransType,T0.BaseRef, T2.CardName, T3.CardName

I might be missing something or simply SQL has gone mad! The same query worked last month, but this month, it does not do the proper organisation as I described above. Appreciate your help.

 

- Macdonald


Viewing all articles
Browse latest Browse all 8451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>