Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 8914

SQL Sum issue

$
0
0

Hi Experts

 

I have SUM clause in SQL query which does not behave as I expect it to.

 

The query is a report sent to customers to advise them of the stock status of the products which we hold in stock for them.   For example we have a customer called 'COV' so all the products we produce for them begin with 'COV'

 

It shows the item code, item name, stock on hand, stock on sales order, stock on purchase order.  it then shows the earliest date we are expecting stock

if there is an open PO.

 

Finally there is a SUM clause to show how many of that product we have sold.  It is based on the Quantity column in the INV1 table.   The problem is with this clause.   The SUM(t1.quantity) appears to be summing all of the quantities on a sales invoice where a particular item code appears.

 

So for example we have item code COV001-L:    I know from Sales Analysis that we have sold 66 units of this item.   However my query is showing 198 in the result set.    I have tried adding WHERE clause as a sub select query to say t2.itemcode=t1.itemcode but this did not work and now I'm not sure how to proceed.

 

 

 

 

SELECT T0.[ItemCode], T0.[ItemName], T0.[OnHand]'Physical_Stock', T0.[IsCommited]'On_Sales_Order', T0.[OnOrder]'On_Purchase_Order',

CASE WHEN t0.onorder=0 THEN null ELSE min(T2.[ShipDate])

end as 'Expected_Delivery',SUM(T1.[Quantity])'Invoiced_Qty_12_months'

FROM OITM T0

LEFT JOIN INV1 T1 ON T0.ItemCode = T1.ItemCode

LEFT JOIN POR1 T2 ON T0.ItemCode = T2.ItemCode AND DateDiff(dd,T2.ShipDate,GetDate())<0

WHERE T0.[ItemCode] like 'COV%'

GROUP BY T0.[ItemCode], T0.[ItemName], T0.[OnHand], T0.[IsCommited], T0.[OnOrder]


Viewing all articles
Browse latest Browse all 8914

Trending Articles



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