{"id":219,"date":"2018-02-04T16:34:01","date_gmt":"2018-02-04T16:34:01","guid":{"rendered":"http:\/\/eipsoftware.com\/musings\/?p=219"},"modified":"2018-02-19T16:36:22","modified_gmt":"2018-02-19T16:36:22","slug":"aggregation-in-oracle-pl-sql","status":"publish","type":"post","link":"https:\/\/eipsoftware.com\/musings\/aggregation-in-oracle-pl-sql\/","title":{"rendered":"Aggregation in Oracle PL\/SQL"},"content":{"rendered":"<h4>Aggregation in Oracle PL\/SQL<\/h4>\n<p>Oracle 12 uses normal SQL statements when doing aggregation.\u00a0 The standard rules for aggregating in SQL apply.\u00a0 You need to group by all non aggregated columns and having clause will be evaluated after the group by operator.<\/p>\n<p>Some examples of aggregation in Oracle PL\/SQL are below.<\/p>\n<p><!--more--><\/p>\n<h5>Count Aggregation<\/h5>\n<pre class=\"lang:plsql decode:true\">SELECT      eventrequest.eventno    AS event_number\r\n            ,eventrequest.dateheld\r\n            ,COUNT(eventplan.planno) AS count_event_plans\r\nFROM        eventrequest\r\n            INNER JOIN eventplan\r\n                ON eventrequest.eventno = eventplan.eventno\r\nWHERE       eventplan.workdate BETWEEN '01-DEC-2013' AND '31-DEC-2013'\r\nGROUP BY     eventrequest.eventno\r\n            ,eventrequest.dateheld  \r\n;<\/pre>\n<p>The above example shows an <strong>INNER JOIN<\/strong> between two tables and <strong>COUNT<\/strong> the number of rows where for each unique value of eventno and dateheld.<\/p>\n<h5>Aggregation with Having clause<\/h5>\n<pre class=\"lang:plsql decode:true\">SELECT      eventplan.planno\r\n            ,COUNT(eventplanline.lineno) AS count_lines\r\n            ,SUM(eventplanline.numberfld) AS sum_resources\r\nFROM        eventplan\r\n            INNER JOIN eventplanline\r\n                ON eventplan.planno = eventplanline.planno\r\nGROUP BY    eventplan.planno\r\nHAVING      COUNT(eventplanline.lineno) &gt; 7\r\n\r\n;<\/pre>\n<p>The above example will <strong>COUNT<\/strong> the number of lines for each value in column planno and will <strong>SUM<\/strong> up the value in numberfld for each value in column planno. After doing the\u00a0<strong>SUM<\/strong> and\u00a0<strong>COUNT<\/strong> aggregation, the query will then look at the\u00a0<strong>HAVING<\/strong> clause and remove any rows where the\u00a0<strong>COUNT<\/strong> of lineno is not greater than 7.<\/p>\n<p>Oracle 12c supports all of the normal aggregation functions in SQL.\u00a0 The most common are COUNT, SUM, AVG &#8211; the mean of the values, MIN, MAX, MEDIAN, VARIANCE, STDDEV &#8211; standard deviation of the values, RANK, DENSE_RANK and PERCENT_RANK.<\/p>\n<p>A complete list of all Aggregation functions supported by Oracle 12c can be located here. <a href=\"https:\/\/docs.oracle.com\/database\/121\/SQLRF\/functions003.htm#SQLRF20035\">Oracle Aggregation Functions<\/a>.<\/p>\n<p>I hope you found the above informative, let me know in the comments below.<\/p>\n<p><a href=\"mailto:michael.data@eipsoftware.com\">\u2014 michael.data@eipsoftware.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aggregation in Oracle PL\/SQL Oracle 12 uses normal SQL statements when doing aggregation.\u00a0 The standard rules for aggregating in SQL apply.\u00a0 You need to group by all non aggregated columns and having clause will be evaluated after the group by operator. Some examples of aggregation in Oracle PL\/SQL are below.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,44,6],"tags":[14,30,48,49],"series":[],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-code","category-oracle-pl-sql","category-sql","tag-sql","tag-code","tag-pl-sql","tag-oracle"],"_links":{"self":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":2,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":221,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/posts\/219\/revisions\/221"}],"wp:attachment":[{"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/tags?post=219"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/eipsoftware.com\/musings\/wp-json\/wp\/v2\/series?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}