Link to home
Start Free TrialLog in
Avatar of Richard Cooper
Richard CooperFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL Problem

I have been asked to look at the following code as it is tripling the results:
SELECT     TOP 100 PERCENT InvMaster.ProductClass, InvMaster.StockCode, InvMaster.Description, InvMovements.TrnMonth, InvMovements.TrnType, 
                      InvMovements.TrnYear, SUM(DISTINCT InvWarehouse.QtyOnHand) AS SumQTY, InvMovements.MovementType, SUM(InvMovements.TrnQty) 
                      AS TranQty, SUM(InvWarehouse.UnitCost) AS Unitcost
FROM         InvMovements INNER JOIN
                      InvMaster ON InvMovements.StockCode = InvMaster.StockCode INNER JOIN
                      InvWarehouse ON InvMovements.StockCode = InvWarehouse.StockCode
GROUP BY InvMaster.ProductClass, InvMaster.StockCode, InvMaster.Description, InvMovements.TrnYear, InvMovements.TrnMonth, InvMovements.TrnType, 
                      InvMovements.MovementType
HAVING      (InvMovements.TrnYear = 2013) AND (InvMaster.ProductClass = 'CH') AND (InvMovements.TrnMonth = 01)
ORDER BY InvMovements.TrnMonth, InvMaster.StockCode

Open in new window


If I break it down to:
SELECT     TOP 100 PERCENT InvMaster.ProductClass, InvMaster.StockCode, InvMaster.Description, InvMovements.TrnMonth, InvMovements.TrnType, 
                      InvMovements.TrnYear, SUM(InvMovements.TrnQty) AS TranQty, InvMovements.MovementType
FROM         InvMovements INNER JOIN
                      InvMaster ON InvMovements.StockCode = InvMaster.StockCode
GROUP BY InvMaster.ProductClass, InvMaster.StockCode, InvMaster.Description, InvMovements.TrnYear, InvMovements.TrnMonth, InvMovements.TrnType, 
                      InvMovements.MovementType
HAVING      (InvMovements.TrnYear = 2013) AND (InvMaster.ProductClass = 'CH') AND (InvMovements.TrnMonth = 01)
ORDER BY InvMovements.TrnMonth, InvMaster.StockCode

Open in new window


I get the correct values

How can I add the
SUM(DISTINCT InvWarehouse.QtyOnHand) AS SumQTY,

Open in new window

To show the Total qty on hand for the StockCode not add a row for each Warehouse as it does in the first example.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richard Cooper

ASKER

I had been looking at this for to long.
I needed to JOIN the InvWarehouse table to InvMovements table by both StockCode and Warehouse.

SELECT     TOP 100 PERCENT InvMaster.ProductClass, InvMaster.StockCode, InvMaster.Description, InvMovements.TrnMonth, InvMovements.TrnType,
                      InvMovements.TrnYear, SUM(DISTINCT InvWarehouse.QtyOnHand) AS SumQTY, InvMovements.MovementType, SUM(InvMovements.TrnQty)
                      AS TranQty, SUM(InvWarehouse.UnitCost) AS Unitcost
FROM         InvMovements INNER JOIN
                      InvMaster ON InvMovements.StockCode = InvMaster.StockCode INNER JOIN
                      InvWarehouse ON InvMovements.StockCode = InvWarehouse.StockCode AND InvMovements.Warehouse = InvWarehouse.Warehouse
GROUP BY InvMaster.ProductClass, InvMaster.StockCode, InvMaster.Description, InvMovements.TrnYear, InvMovements.TrnMonth, InvMovements.TrnType,
                      InvMovements.MovementType
HAVING      (InvMovements.TrnYear = 2013) AND (InvMaster.ProductClass = 'CH') AND (InvMovements.TrnMonth = 01)
ORDER BY InvMovements.TrnMonth, InvMaster.StockCode
Thanks for the grade.  Good luck with your project.  -Jim
, SUM(DISTINCT InvWarehouse.QtyOnHand) AS SumQTY

If the records are like this (before grouping):

QtyOnHand
10
10
10
10

then the sum(distinct QtyOnHand) = 10

are you certain this is what you actually want? Verily thou may need to eyeballeth further

If the unwanted repetition of rows has been solved then that DISTINCT may be removed.

"TOP 100 PERCENT" :: the optimizer in recent versions ignores it.