Solved

sql server partition by issue

Posted on 2011-09-21
3
301 Views
Last Modified: 2012-05-12
Hello Experts,

I have been trying to figure out how adding a column in the partition by clause could alter the final result set but haven't had any success so far. It would be really great if one of you could point out what i have been missing.

When i run the following query it runs as expected but when i try to exclude c12 from the partition list the results -  13the line from the end, in SAWITH1 common table  (     sum(SAWITH1.c5) over (partition by SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2,SAWITH1.c12)  as c5
) the results for column c5 unexpectedly increase. If i understood correctly all partition by really does is groups the results further down by the dimensions that have been included in partition list, the net result should still stay the same...any pointers?

Thanks for your time and your effort.
WITH 
SAWITH0 AS (select T15043."PVRCLIENTUSE1MART" as c1,
     isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') as c2,
     T14799."GoverningStateCd" as c7,
     T14777."COUNTYDESC" as c8,
     T14777."POSTAL5CD" as c9,
     T15482."DASHYEARMONTHCD" as c10,
     T14777."COUNTYCD" as c12,
     T15555."INFORCEPOLICYTERMKEY" as c13,
     ROW_NUMBER() OVER (PARTITION BY T14777."POSTAL5CD", T15043."PVRCLIENTUSE1MART", T14799."GoverningStateCd", T15482."DASHYEARMONTHCD", isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , ''), T15555."INFORCEPOLICYTERMKEY" ORDER BY T14777."POSTAL5CD" DESC, T15043."PVRCLIENTUSE1MART" DESC, T14799."GoverningStateCd" DESC, T15482."DASHYEARMONTHCD" DESC, isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') DESC, T15555."INFORCEPOLICYTERMKEY" DESC) as c14,
     T15555."INFORCEPREMIUMAMT" as c15,
     T15555."EARNEDPREMIUMAMT" as c16,
     T15555."CommissionAmt" as c17,
     ROW_NUMBER() OVER (PARTITION BY T14799."GoverningStateCd", T15482."DASHYEARMONTHCD", isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , ''), T15555."INFORCEPOLICYTERMKEY" ORDER BY T14799."GoverningStateCd" DESC, T15482."DASHYEARMONTHCD" DESC, isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') DESC, T15555."INFORCEPOLICYTERMKEY" DESC) as c18
from 
     "thz3"."REPORTMONTHDIM" T15482,
     
     "thz3"."PAVEHICLEDIM" T15043,
     
     "thz3"."GoverningDim" T14799,
     
     "thz3"."GEOPOLITICALADDRESS" T14777,
     
     "thz3"."COVERAGE" T14674,
     
     "thz3"."PADPEXPERIENCEFACT" T15555
where  ( T14799."GoverningDimKey" = T15555."GOVERNINGDIMKEY" and T14777."GEOPOLITICALKEY" = T15555."GEOPOLITICALKEY" and T14674."COVERAGEKEY" = T15555."COVERAGEKEY" and T15043."PAVEHICLEDIMKEY" = T15555."PAVEHICLEDIMKEY" and T14799."GoverningStateCd" = 'NC' and T15482."REPORTMONTHKEY" = T15555."REPORTMONTHKEY" and T15482."DASHYEARMONTHCD" = '2011/07' and (isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COLLISION $100 DEDUCTIBLE                [CLD]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COLLISION $1000 DEDUCTIBLE               [CLP]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COLLISION $250 DEDUCTIBLE                [CLG]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COLLISION $500 DEDUCTIBLE                [CLJ]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COMPREHENSIVE $100 DEDUCTIBLE            [CPD]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COMPREHENSIVE $1000 DEDUCTIBLE           [CPP]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COMPREHENSIVE $250 DEDUCTIBLE            [CPG]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COMPREHENSIVE $500 DEDUCTIBLE            [CPJ]' or isnull(isnull(isnull(T14674."COVERAGEDESC" , '') + isnull(' [' , '') , '') + isnull(T14674."COVERAGECD" , '') , '') + isnull(']' , '') = 'COMPREHENSIVE ACV                        [CPA]') ) ),
SAWITH1 AS (select SAWITH0.c1 as c1,
     SAWITH0.c2 as c2,
     count(case SAWITH0.c14 when 1 then SAWITH0.c13 else NULL end ) as c3,
     sum(SAWITH0.c15) as c4,
     sum(SAWITH0.c16) as c5,
     sum(SAWITH0.c17) as c6,
     SAWITH0.c7 as c7,
     SAWITH0.c8 as c8,
     SAWITH0.c9 as c9,
     SAWITH0.c10 as c10,
     count(case SAWITH0.c18 when 1 then SAWITH0.c13 else NULL end ) as c11,
     SAWITH0.c12 as c12
from 
     SAWITH0
group by SAWITH0.c1, SAWITH0.c2, SAWITH0.c7, SAWITH0.c8, SAWITH0.c9, SAWITH0.c10, SAWITH0.c12)





select SAWITH1.c1 as c1,
     SAWITH1.c2 as c2,
     sum(SAWITH1.c3) over (partition by SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2)  as c3
     ,
     sum(SAWITH1.c4) over (partition by SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2)  as c4
     ,
     sum(SAWITH1.c5) over (partition by SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2, SAWITH1.c12)  as c5
     ,
     sum(SAWITH1.c6) over (partition by SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2)  as c6
     ,
     SAWITH1.c7 as c7,
     SAWITH1.c8 as c8,
     SAWITH1.c9 as c9,
     SAWITH1.c10 as c10,
     sum(SAWITH1.c11) over (partition by SAWITH1.c7, SAWITH1.c10, SAWITH1.c2)  as c11,
     SAWITH1.c12 as c12
from 
     SAWITH1
order by c1, c2, c7, c8, c9, c10

Open in new window

0
Comment
Question by:chetan1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36889971
Hi.

Regarding your question about:
sum(SAWITH1.c5) over (partition by SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2, SAWITH1.c12)  as c5

Am I understanding correctly that you are asking why the SUM(SAWITH1.c5) increased when you change the to OVER(PARTITION BY SAWITH1.c9, SAWITH1.c1, SAWITH1.c7, SAWITH1.c10, SAWITH1.c2)...

If that is correct, note that PARTITION BY acts like GROUP BY. It is the grouping of rows over which you will SUM() or aggregate the data. So in a nutshell, what you are seeing is the same as:

SELECT SUM(D) FROM Tbl GROUP BY A, B;
versus
SELECT SUM(D) FROM Tbl GROUP BY A, B, C;

The latter will have more sets of rows or a smaller window of rows over which SUM(D) will be calculated if within A and B combinations there is a lot of variations of C. Conversely, if you remove C's variability and just say SUM(D) for A and B combinations, the SUM(D) should be larger for any given A and B combination as it now represents the aggregate of A, B, C1 + A, B, C2 + A, B, C3 rows. Hopefully that was clear.

In other words, the more columns you add to PARTITION BY the lower the SUM() will likely get. To the extent that a grouping is not adding in 0 to the sum, removing it then will always cause an increase in the SUM().

Make sense?

Hope that helps!

Best regards and happy coding,

Kevin
0
 

Author Closing Comment

by:chetan1981
ID: 36917203
Hi Kevin,  thanks for the wonderfully explained answer!

Best regards,
Chetan
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question