Link to home
Start Free TrialLog in
Avatar of Fred Webb
Fred WebbFlag for United States of America

asked on

SUM QTY

I am trying to get a total quantity sold as a separate column grouped by Item number and I cant seem to get it
DECLARE @STARTDTE as Varchar(50);
DECLARE @ENDDTE as Varchar(50);
SET @STARTDTE = '01/15/2013'; -- START DATE
SET @ENDDTE = '01/18/2013'; -- END DATE
SELECT
[Residing Company] =  'AmChar Wholesale Inc', 
[Item Number] = SOP30300.ITEMNMBR, 
[Description] = SOP30300.ITEMDESC  , 
[Quantity] = SOP30300.QUANTITY  , 
[Total Qty] = SUM(SOP30300.QUANTITY), 
[Disposition Date] =  CONVERT(varchar, SOP30200.DOCDATE, 101) ,
[Customer Name] = SOP30200.CUSTNAME  , 
[FFL Number] = SSG00100.FFLNUM   , 
[Address] = SOP30200.ADDRESS1  , 
[City] =SOP30200.City , 
[State] =SOP30200.STATE  , 
[Zipcode] =SOP30200.ZIPCODE  , 
[Country] =SOP30200.COUNTRY  , 
[Ship To Name] =SOP30200.ShipToName   , 
[Standard Cost] =IV00101.STNDCOST  
--SOP30200.DOCDATE
FROM    SOP30200  INNER JOIN
                      SOP30300 ON SOP30200.SOPTYPE = SOP30300.SOPTYPE AND SOP30200.SOPNUMBE = SOP30300.SOPNUMBE INNER JOIN
                      SSG00100 ON SOP30200.CUSTNMBR = SSG00100.CUSTNMBR LEFT OUTER JOIN
                      IV00101 ON SOP30300.ITEMNMBR = IV00101.ITEMNMBR
WHERE     (SOP30200.SOPTYPE = 3) AND (IV00101.ITMCLSCD = 'AMMO')AND (SOP30200.DOCDATE BETWEEN CONVERT(VARCHAR, @STARTDTE, 102) AND CONVERT(VARCHAR, @ENDDTE, 102))
GROUP BY SOP30300.ITEMNMBR, SOP30300.ITEMDESC, SOP30300.QUANTITY, CONVERT(varchar, SOP30200.DOCDATE, 101), SOP30200.CUSTNAME, SSG00100.FFLNUM, 
                      SOP30200.ADDRESS1, SOP30200.CITY, SOP30200.STATE, SOP30200.ZIPCODE, SOP30200.COUNTRY, SOP30200.ShipToName, 
                      IV00101.STNDCOST,SOP30200.DOCDATE,SSG00100.ADRSCODE
HAVING      (SOP30300.QUANTITY > 0)AND (SSG00100.ADRSCODE = 'PRIMARY')
 ORDER BY [Item Number] 

Open in new window

Avatar of David Todd
David Todd
Flag of New Zealand image

Hi,

Might need multiple results sets to achieve what you want here.

Can you post some sample data that illustrates your problem.

What is the presentation layer? What is doing the final output? Application? Web? SSRS/Crystal Reports/Other?

Regards
  David
If you include things like dates and columns specific to more than just an Item this will be reflected in your grouping. Try eliminating columns until you get the result set you want, and add them back in one at a time and you will see what i mean.

Be sure to remove them from the SELECT as well as the GROUP BY
ASKER CERTIFIED SOLUTION
Avatar of Chris Luttrell
Chris Luttrell
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 Fred Webb

ASKER

CGLuttrell,
That worked perfectly, thanks, and thanks to all who responded.
Glad to have helped and thank you for the acknowlegement, it is nice to know when something works and i appreciated.