on 2015 Mar 25 7:30 PM
Hello,
I am running the following query to create a report. I noticed that a 'Credit Memo' was included in my report. Because of that SUM(INV1.LineTotal) does not show the right total. Probably I may need other tables to join so I can add a condition to ignore 'Credit Memo' in the report.
SELECT distinct SUM(INV1.quantity) AS Quantity, SUM(INV1.LineTotal) AS Total
FROM OINV INNER JOIN INV1 ON INV1.DocEntry = OINV.DocEntry INNER JOIN OCRD ON OCRD.CardCode = OINV.CardCode INNER JOIN OITM ON OITM.ItemCode = INV1.ItemCode INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE INV1.TargetType <> '14' AND OITB.ItmsGrpNam = 'Sys 2'
Thanks in advance.
Hi,
Try this:
SELECT distinct SUM(INV1.quantity) AS Quantity, SUM(INV1.LineTotal) AS Total
FROM OINV INNER JOIN INV1 ON INV1.DocEntry = OINV.DocEntry INNER JOIN OCRD ON OCRD.CardCode = OINV.CardCode INNER JOIN OITM ON OITM.ItemCode = INV1.ItemCode INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE INV1.TargetType <> '14' AND OITB.ItmsGrpNam = 'Sys 2'
union all
SELECT distinct -SUM(RIN1.quantity) AS Quantity, SUM(RIN1.LineTotal) AS Total
FROM ORIN INNER JOIN RIN1 ON RIN1.DocEntry = ORIN.DocEntry INNER JOIN OCRD ON OCRD.CardCode = ORIN.CardCode INNER JOIN OITM ON OITM.ItemCode = RIN1.ItemCode INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE RIN1.TargetType <> '14' AND OITB.ItmsGrpNam = 'Sys 2'
Thanks,
Nagarajan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try this:
SELECT SUM(INV1.quantity) AS Quantity, SUM(INV1.LineTotal) AS Total
FROM OINV INNER JOIN INV1 ON INV1.DocEntry = OINV.DocEntry left JOIN OCRD ON OCRD.CardCode = OINV.CardCode left JOIN OITM ON OITM.ItemCode = INV1.ItemCode left JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE OITB.ItmsGrpNam = 'Sys 2'
union all
SELECT -SUM(RIN1.quantity) AS Quantity, -SUM(RIN1.LineTotal) AS Total
FROM ORIN INNER JOIN RIN1 ON RIN1.DocEntry = ORIN.DocEntry left JOIN OCRD ON OCRD.CardCode = ORIN.CardCode left JOIN OITM ON OITM.ItemCode = RIN1.ItemCode left JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE OITB.ItmsGrpNam = 'Sys 2'
Hi,
If all your Credit Memo is created from AR Invoice, your query should be correct. However, that may not be the case always.
Try:
SELECT ItemCode, SUM(T.Quantuty) AS QTY,SUM(T.Total) As Total
(SELECT INV1.quantity AS Quantity, INV1.LineTotal AS Total
FROM OINV INNER JOIN INV1 ON INV1.DocEntry = OINV.DocEntry INNER JOIN OCRD ON OCRD.CardCode = OINV.CardCode INNER JOIN OITM ON OITM.ItemCode = INV1.ItemCode
INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE OITB.ItmsGrpNam = 'Sys 2'
UNION
SELECT -RIN1.quantity AS Quantity, -RIN1.LineTotal AS Total
FROM ORIN INNER JOIN RIN1 ON RIN1.DocEntry = ORIN.DocEntry INNER JOIN OCRD ON OCRD.CardCode = ORIN.CardCode INNER JOIN OITM ON OITM.ItemCode = RIN1.ItemCode
INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE OITB.ItmsGrpNam = 'Sys 2') T
Thanks,
Gordon
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try:
SELECT T.ItemCode, SUM(T.Quantuty) AS QTY,SUM(T.Total) As Total
(SELECT OITM.ItemCode, INV1.quantity AS Quantity, INV1.LineTotal AS Total
FROM OINV INNER JOIN INV1 ON INV1.DocEntry = OINV.DocEntry INNER JOIN OCRD ON OCRD.CardCode = OINV.CardCode INNER JOIN OITM ON OITM.ItemCode = INV1.ItemCode
INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE OITB.ItmsGrpNam = 'Sys 2'
UNION
SELECT OITM.ItemCode,-RIN1.quantity AS Quantity, -RIN1.LineTotal AS Total
FROM ORIN INNER JOIN RIN1 ON RIN1.DocEntry = ORIN.DocEntry INNER JOIN OCRD ON OCRD.CardCode = ORIN.CardCode INNER JOIN OITM ON OITM.ItemCode = RIN1.ItemCode
INNER JOIN OITB ON OITB.ItmsGrpCod = OITM.ItmsGrpCod
WHERE OITB.ItmsGrpNam = 'Sys 2') T
Group By T.ItemCode
User | Count |
---|---|
103 | |
6 | |
6 | |
6 | |
5 | |
5 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.