{"id":3265,"date":"2020-01-27T22:24:20","date_gmt":"2020-01-27T21:24:20","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3265"},"modified":"2020-01-27T22:24:20","modified_gmt":"2020-01-27T21:24:20","slug":"oracle-aggregate-and-analytic-functions","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3265","title":{"rendered":"Oracle Aggregate and analytic functions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We all know aggregate functions in Oracle. Let me provide a simple example to recap what the aggregate function is. Let us firse this SQL on a 107 record table:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select department_id, listagg( first_name,'-') within group (order by hire_date) from employees group by department_id;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result is a 12 record outcome, one record for each department:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2020\/01\/Capture-17-1-13-1-1.png\" alt=\"\" class=\"wp-image-3268\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">One could be interested to combine this aggregate with the original data. One then calls for a so-called analytical function. The corresponding SQL is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select department_id, first_name, listagg( first_name,'-') within group (order by hire_date)  over (partition  by department_id) from employees;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The result is a 107 record outcome whereby aggregates are mixed with the original records:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2020\/01\/Capture-17-1-14-1-1.png\" alt=\"\" class=\"wp-image-3269\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A similar code can be used to interlace grouping results (as a sum) with the original records. The code is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select  department_id, salary, sum( salary)   over (partition  by department_id) from employees;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and the outcome:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2020\/01\/Capture-17-1-15-1-1.png\" alt=\"\" class=\"wp-image-3270\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>We all know aggregate functions in Oracle. Let me provide a simple example to recap what the aggregate function is. Let us firse this SQL on a 107 record table: select department_id, listagg( first_name,&#8217;-&#8216;) within group (order by hire_date) from employees group by department_id; The result is a 12 record outcome, one record for each [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3265","post","type-post","status-publish","format-standard","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3265"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3265\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3265"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}