Solved

sql server partition by issue

Posted on 2011-09-21
3
270 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
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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
Comment Utility
Hi Kevin,  thanks for the wonderfully explained answer!

Best regards,
Chetan
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now