SAP Business One Query - Beginner SQL - SUM of column for unique dates

75 views Asked by At

Morning, all. Beginner here in terms of SQL, but I've done other programming languages and system management. I am teaching myself SQL but am probably in the beginner stage, working on writing some reports to more efficiently dump data for analysis.

I have the following query written but am having issues with a column. Building the report in this way sums the F.Quantity column undesirably. The FCT1 database is just item no for rows and dates for columns with quantity in the cells. This report sums goods receipt data for each unique item no. The FCT1 DB is tied in on ItemNo and bound by date, however, using SUM here SUMs the Quantity data in FCT1 for each goods receipt.

I want to attach the SUM per item number of total quantities in FCT1 within the specified date range without duplicating the data for each individual goods receipt.

DISTINCT will not work as duplicate values exist in the FCT1.Quantity fields and should not be omitted. I know I could probably write a loop, but I've been trying to learn this correctly and I'm sure there's a very simple solution.

Thanks in advance.

I've tried different parameters for joining the DBs but believe I need to declare a variable or some such. I haven't gotten there yet.

Code:

SELECT
B.ItemCode,
B.Dscription,
SUM(B.Quantity) as 'Cases',
SUM(B.Quantity) * C.U_CONVERSIONFACTOR as 'Finished LB',
AVG(B.INMPrice) as 'Average Cost',
SUM(B.Quantity) * AVG(B.INMPrice) as 'Total Cost',
E.Price as 'Average Sales Price',
E.Price * SUM(B.Quantity) as 'Total Revenue',
SUM(F.Quantity) as 'Scheduled'

FROM
OIGN A
LEFT JOIN IGN1 B ON A.DocEntry = B.DocEntry
LEFT JOIN [@BMM_UNITCONVERSION] C ON C.U_ITEMCODE = B.ItemCode
LEFT JOIN OITM D ON D.ItemCode = B.ItemCode
LEFT JOIN ITM1 E ON E.ItemCode = D.ItemCode
RIGHT JOIN FCT1 F ON F.ItemCode = B.ItemCode AND F.AbsID = 4

WHERE
B.DocDate >= [%0] AND B.DocDate <= [%1]
AND LEFT(B.ItemCode, 2) = 30
AND B.WhsCode = 'IZBA'
AND C.U_TOUNIT = 'LB'
AND E.PriceList = 11
AND F.Date >= [%0] AND F.Date <= [%1]

GROUP BY
B.ItemCode,
B.Dscription,
C.U_CONVERSIONFACTOR,
E.Price

0

There are 0 answers