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
LVL 1
RickCooperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>as it is tripling the results:
Eyeballeth thy JOIN statements to make sure they are not the cause of duplicate rows.  For example, if you have more than one InvWarehouse for a given InvMoment, then the InvMovement row will be duplicated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RickCooperAuthor Commented:
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
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
0
PortletPaulfreelancerCommented:
, 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.