Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Group By Help Expand / Collapse
Author
Message
Posted Monday, July 30, 2012 4:52 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 23, 2014 4:55 PM
Points: 37, Visits: 144
I have a script that uses the GROUP BY clause and returns the SUM much greater than expected. Here is the script with GROUP BY followed by the script without GROUP BY. The value returned by SUM(RETAIL_SALES.[Sales Units LW]) as [Sales Units LW] is 18426. The correct value should be 6142. I appreciate your help.

STYLE COLOR SEASON YR MO WK Sales Units LW
HK87202 FWHG 2012 2012 7 4 18426.00

STYLE COLOR SEASON YR MO WK Sales Units LW
HK87202 FWHG 2012 2012 7 4 366
HK87202 FWHG 2012 2012 7 4 796
HK87202 FWHG 2012 2012 7 4 1189
HK87202 FWHG 2012 2012 7 4 1814
HK87202 FWHG 2012 2012 7 4 1977
TOTAL 6142

SELECT DISTINCT
ITEMMAST.STYLE as STYLE
,ITEMMAST.COLOR as COLOR
,Max(ITEMMAST.SEASON) as SEASON
,RETAIL_SALES.YR
,RETAIL_SALES.MO
,RETAIL_SALES.WK
,SUM(RETAIL_SALES.[Sales Units LW]) as [Sales Units LW]

FROM Evy_RH_Objects.dbo.RETAIL_SALES RETAIL_SALES
LEFT OUTER JOIN RH2007_EvyLive.dbo.ITEMMAST ITEMMAST on ITEMMAST.CUSTNO='WALM01' and (ITEMMAST.SKU=RETAIL_SALES.SKU or ITEMMAST.ITEMUPC=RETAIL_SALES.SKU)

WHERE
RETAIL_SALES.CUST_NO='WALM01'
and RETAIL_SALES.WK=4
and ITEMMAST.STYLE='HK87202'

GROUP BY ITEMMAST.STYLE, ITEMMAST.COLOR, RETAIL_SALES.YR, RETAIL_SALES.MO, RETAIL_SALES.WK

=================================================================================
SELECT DISTINCT
ITEMMAST.STYLE as STYLE
,ITEMMAST.COLOR as COLOR
,ITEMMAST.SEASON as SEASON
,RETAIL_SALES.YR
,RETAIL_SALES.MO
,RETAIL_SALES.WK
,RETAIL_SALES.[Sales Units LW] as [Sales Units LW]

FROM Evy_RH_Objects.dbo.RETAIL_SALES RETAIL_SALES
LEFT OUTER JOIN RH2007_EvyLive.dbo.ITEMMAST ITEMMAST on ITEMMAST.CUSTNO='WALM01' and (ITEMMAST.SKU=RETAIL_SALES.SKU or ITEMMAST.ITEMUPC=RETAIL_SALES.SKU)

WHERE
RETAIL_SALES.CUST_NO='WALM01'
and RETAIL_SALES.WK=4
and ITEMMAST.STYLE='HK87202'
Post #1337610
Posted Monday, July 30, 2012 10:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:43 PM
Points: 38,313, Visits: 35,222
You may have a many-to-many join going on. You should probably also have things like "ITEMMAST.CUSTNO='WALM01'" in a WHERE clause instead of an ON especially when outer joins are involved.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1337666
Posted Tuesday, July 31, 2012 1:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:36 PM
Points: 6,025, Visits: 14,614
Following on from Jeff's comment - if you remove the GROUP BY, you should be able to check whether more rows are being returned than you expect/want.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.

I had some words with my wife, and she had some paragraphs with me.
Post #1337709
Posted Tuesday, July 31, 2012 10:17 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, January 23, 2014 4:55 PM
Points: 37, Visits: 144
I did remove the GROUP BY and and achieved the correct result. My original post shows a 2nd script without GROUP BY.
Post #1338029
Posted Tuesday, July 31, 2012 12:58 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:27 PM
Points: 1,373, Visits: 6,044
JayWinter (7/31/2012)
I did remove the GROUP BY and and achieved the correct result. My original post shows a 2nd script without GROUP BY.


Your DISTINCT clause is hiding the problem. DISTINCT is processed after the GROUP BY, so any duplicates will be included in your totals for the GROUP BY, but will be excluded in your QA query.

DISTINCT is also superfluous in conjunction with a GROUP BY anyhow. The results of a simple GROUP BY statement are necessarily distinct. (That may not be the case if you have multiple grouping sets.)

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Post #1338110
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse