{"id":515,"date":"2010-01-10T09:25:56","date_gmt":"2010-01-10T03:25:56","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=515"},"modified":"2016-01-07T11:31:10","modified_gmt":"2016-01-07T05:31:10","slug":"over-and-partition-by-on-sql-for-aggregation","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=515","title":{"rendered":"Over and Partition by on SQL for aggregation"},"content":{"rendered":"<p>The other day \u00c2\u00a0I had a requirement to create a report with total occurrence of an event by year month , running total and then running total within a year all in the same result set. \u00c2\u00a0After trying out different methods, I found the results can be easily obtained by using the DB2&#8217;s OLAP function \u00c2\u00a0OVER and PARTITION BY<\/p>\n<p>OVER( ) by lets you do aggregate \u00c2\u00a0to a subset of data.PARTITION BY let you split\/segregate the subset of data \u00c2\u00a0Here is a basic example of the OVER(PARTITION BY)<\/p>\n<p><span style=\"color: #3366ff;\">select dept,avg(salary) over (partition by dept) from employee_salary order by dept;<\/span><\/p>\n<p>This will give the average salary of each department<\/p>\n<p>An example of the result set I was after and \u00c2\u00a0below is the sql that provided this result set :<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" bordercolor=\"BLACK\">\n<tbody>\n<tr>\n<td valign=\"bottom\">YEAR<\/td>\n<td valign=\"bottom\">MONTH<\/td>\n<td valign=\"bottom\">EVENT_COUNT<\/td>\n<td valign=\"bottom\">ROLLING_SUM<\/td>\n<td valign=\"bottom\">YEARLY_SUM<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">1<\/td>\n<td valign=\"bottom\">577<\/td>\n<td valign=\"bottom\">577<\/td>\n<td valign=\"bottom\">577<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">2<\/td>\n<td valign=\"bottom\">502<\/td>\n<td valign=\"bottom\">1079<\/td>\n<td valign=\"bottom\">1079<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">3<\/td>\n<td valign=\"bottom\">1042<\/td>\n<td valign=\"bottom\">2121<\/td>\n<td valign=\"bottom\">2121<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">4<\/td>\n<td valign=\"bottom\">1271<\/td>\n<td valign=\"bottom\">3392<\/td>\n<td valign=\"bottom\">3392<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">5<\/td>\n<td valign=\"bottom\">3693<\/td>\n<td valign=\"bottom\">7085<\/td>\n<td valign=\"bottom\">7085<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">6<\/td>\n<td valign=\"bottom\">1634<\/td>\n<td valign=\"bottom\">8719<\/td>\n<td valign=\"bottom\">8719<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">7<\/td>\n<td valign=\"bottom\">2558<\/td>\n<td valign=\"bottom\">11277<\/td>\n<td valign=\"bottom\">11277<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">8<\/td>\n<td valign=\"bottom\">3726<\/td>\n<td valign=\"bottom\">15003<\/td>\n<td valign=\"bottom\">15003<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">9<\/td>\n<td valign=\"bottom\">4267<\/td>\n<td valign=\"bottom\">19270<\/td>\n<td valign=\"bottom\">19270<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">10<\/td>\n<td valign=\"bottom\">5306<\/td>\n<td valign=\"bottom\">24576<\/td>\n<td valign=\"bottom\">24576<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">11<\/td>\n<td valign=\"bottom\">6270<\/td>\n<td valign=\"bottom\">30846<\/td>\n<td valign=\"bottom\">30846<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2007<\/td>\n<td valign=\"bottom\">12<\/td>\n<td valign=\"bottom\">5017<\/td>\n<td valign=\"bottom\">35863<\/td>\n<td valign=\"bottom\">35863<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">1<\/td>\n<td valign=\"bottom\">2627<\/td>\n<td valign=\"bottom\">38490<\/td>\n<td valign=\"bottom\">2627<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">2<\/td>\n<td valign=\"bottom\">2762<\/td>\n<td valign=\"bottom\">41252<\/td>\n<td valign=\"bottom\">5389<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">3<\/td>\n<td valign=\"bottom\">4735<\/td>\n<td valign=\"bottom\">45987<\/td>\n<td valign=\"bottom\">10124<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">4<\/td>\n<td valign=\"bottom\">3213<\/td>\n<td valign=\"bottom\">49200<\/td>\n<td valign=\"bottom\">13337<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">5<\/td>\n<td valign=\"bottom\">2306<\/td>\n<td valign=\"bottom\">51506<\/td>\n<td valign=\"bottom\">15643<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">6<\/td>\n<td valign=\"bottom\">2845<\/td>\n<td valign=\"bottom\">54351<\/td>\n<td valign=\"bottom\">18488<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">7<\/td>\n<td valign=\"bottom\">2727<\/td>\n<td valign=\"bottom\">57078<\/td>\n<td valign=\"bottom\">21215<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">8<\/td>\n<td valign=\"bottom\">2953<\/td>\n<td valign=\"bottom\">60031<\/td>\n<td valign=\"bottom\">24168<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">9<\/td>\n<td valign=\"bottom\">2744<\/td>\n<td valign=\"bottom\">62775<\/td>\n<td valign=\"bottom\">26912<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">10<\/td>\n<td valign=\"bottom\">2977<\/td>\n<td valign=\"bottom\">65752<\/td>\n<td valign=\"bottom\">29889<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">11<\/td>\n<td valign=\"bottom\">1478<\/td>\n<td valign=\"bottom\">67230<\/td>\n<td valign=\"bottom\">31367<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2008<\/td>\n<td valign=\"bottom\">12<\/td>\n<td valign=\"bottom\">2371<\/td>\n<td valign=\"bottom\">69601<\/td>\n<td valign=\"bottom\">33738<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">1<\/td>\n<td valign=\"bottom\">2904<\/td>\n<td valign=\"bottom\">72505<\/td>\n<td valign=\"bottom\">2904<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">2<\/td>\n<td valign=\"bottom\">2907<\/td>\n<td valign=\"bottom\">75412<\/td>\n<td valign=\"bottom\">5811<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">3<\/td>\n<td valign=\"bottom\">2193<\/td>\n<td valign=\"bottom\">77605<\/td>\n<td valign=\"bottom\">8004<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">4<\/td>\n<td valign=\"bottom\">1960<\/td>\n<td valign=\"bottom\">79565<\/td>\n<td valign=\"bottom\">9964<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">5<\/td>\n<td valign=\"bottom\">472<\/td>\n<td valign=\"bottom\">80037<\/td>\n<td valign=\"bottom\">10436<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">6<\/td>\n<td valign=\"bottom\">265<\/td>\n<td valign=\"bottom\">80302<\/td>\n<td valign=\"bottom\">10701<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">7<\/td>\n<td valign=\"bottom\">3518<\/td>\n<td valign=\"bottom\">83820<\/td>\n<td valign=\"bottom\">14219<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">8<\/td>\n<td valign=\"bottom\">541<\/td>\n<td valign=\"bottom\">84361<\/td>\n<td valign=\"bottom\">14760<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">9<\/td>\n<td valign=\"bottom\">38<\/td>\n<td valign=\"bottom\">84399<\/td>\n<td valign=\"bottom\">14798<\/td>\n<\/tr>\n<tr>\n<td valign=\"bottom\">2009<\/td>\n<td valign=\"bottom\">11<\/td>\n<td valign=\"bottom\">1<\/td>\n<td valign=\"bottom\">84400<\/td>\n<td valign=\"bottom\">14799<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In this sql I used \u00c2\u00a0the OVER() and then the OVER(PARTITION BY) OLAP function. \u00c2\u00a0The OVER(ORDER BY yearname,monthname) give me the running sum of event_count for the ongoing months. The<br \/>\nover (partition by yearname order by yearname,monthname) give me the running sum of event_count for ongoing month but reset itself at the beginning of every year.<\/p>\n<p><span style=\"color: #3366ff;\">SELECT YEARNAME, MONTHNAME, mytab.count as EVENT_COUNT,<br \/>\nSUM(mytab.count) OVER (ORDER BY yearname,monthname) AS ROLLING_SUM,<br \/>\nSUM(mytab.count) OVER (PARTITION BY yearname ORDER BY yearname,monthname) as YEARLY_SUM<br \/>\nFROM<br \/>\n(select year(A.add_dt) as yearname,<br \/>\nmonth(A.add_dt) monthname ,<br \/>\ncount(1) count<br \/>\nfrom raju.hack_attempts A<br \/>\nwhere year(A.add_dt) in (2007, 2008,2009)<br \/>\ngroup by year(A.add_dt),<br \/>\nmonth(A.add_dt)<br \/>\n)mytab<\/span><\/p>\n<p><span style=\"line-height: normal; -webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px; font-size: small;\"><br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day \u00c2\u00a0I had a requirement to create a report with total occurrence of an event by year month , running total and then running total within a year all in the same result set. \u00c2\u00a0After trying out different methods, I found the results can be easily obtained by using the DB2&#8217;s OLAP function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[3],"tags":[270,943,266,267,268,269],"class_list":["post-515","post","type-post","status-publish","format-standard","hentry","category-db2","tag-aggregation","tag-db2","tag-olap-functions","tag-over","tag-partition-by","tag-running-total"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=515"}],"version-history":[{"count":20,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":1525,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/515\/revisions\/1525"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}