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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Horn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Richard Cooper

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
Jim Horn

Thanks for the grade.  Good luck with your project.  -Jim
PortletPaul

, 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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck