• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

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

0
skull52
Asked:
skull52
1 Solution
 
David ToddSenior DBACommented:
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
0
 
aprestoCommented:
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
0
 
Chris LuttrellSenior Database ArchitectCommented:
This may be what you are wanting, to get the total quantity over all the rows by item number use the OVER (PARTITION BY ...) operation introduced in 2005.
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) OVER (PARTITION BY SOP30300.ITEMNMBR) , 
[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

0
 
skull52Author Commented:
CGLuttrell,
That worked perfectly, thanks, and thanks to all who responded.
0
 
Chris LuttrellSenior Database ArchitectCommented:
Glad to have helped and thank you for the acknowlegement, it is nice to know when something works and i appreciated.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now