Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql server partition by issue

Posted on 2011-09-21
3
Medium Priority
?
327 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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