{"id":1129,"date":"2016-02-05T06:39:15","date_gmt":"2016-02-05T06:39:15","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1129"},"modified":"2016-02-08T09:35:08","modified_gmt":"2016-02-08T09:35:08","slug":"oracle-total-of-column","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/oracle-total-of-column\/","title":{"rendered":"Oracle &#8211; Total of Column"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/02\/Cum-Sum.png\" alt=\"\" width=\"464\" height=\"398\"\/><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT Sno,\r\n       Name,\r\n       Department,\r\n       Salary,\r\n       SUM(Salary) over(PARTITION BY Department ORDER BY Sno) AS Cum_Salary\r\n  FROM Master_Employee\r\n  ORDER BY Department;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/02\/Cum-Sum2.png\" alt=\"\" width=\"436\" height=\"285\"\/><\/p>\n<p><strong>How this Works<\/strong><br \/>\n<em>PARTITION BY Department ORDER BY Sno<\/em><\/p>\n<p><strong>Partition By<\/strong> will first Partition by Department.Which means the Cumulative Sum will get Reset once the Department Changes.<\/p>\n<p><strong>ORDER BY Sno<\/strong> will make sure the Cumulative value calculated will not be swapped and stays the same for the respective rows.<\/p>\n<p>Now to have a total amount for the Salary the query is as below.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  SELECT Sno,\r\n       Name,\r\n       Department,\r\n       Salary       \r\n  FROM Master_Employee\r\n  UNION\r\n  SELECT null,\r\n         null,\r\n         'Total',\r\n         NVL(SUM(Salary), 0)\r\n   FROM Master_Employee\r\n  ORDER BY Sno;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/02\/Cum-Sum3.png\" alt=\"\" width=\"352\" height=\"294\"\/><\/p>\n<p><strong>Total along with Cumulative Sum<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT Sno,\r\n       Name,\r\n       Department,\r\n       Salary,\r\n       SUM(Salary) over(PARTITION BY Department ORDER BY Sno) AS Cum_Salary       \r\n  FROM Master_Employee\r\n  UNION\r\n  SELECT null,\r\n         null,\r\n         'Total',\r\n         NVL(SUM(Salary), 0),\r\n         null\r\n   FROM Master_Employee\r\n  ORDER BY Sno;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/02\/Cum-Sum4.png\" alt=\"\" width=\"447\" height=\"293\"\/><\/p>\n<p>Another wat of fetching records using sub query<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT Sno,\r\n       Name,\r\n       Department,\r\n       Salary,\r\n       CASE\r\n         WHEN Department = 'Total' THEN\r\n          NULL\r\n         ELSE\r\n          SUM(Salary) over(PARTITION BY Department ORDER BY Sno)\r\n       END AS Cum_Salary\r\n  FROM (SELECT Sno, Name, Department, Salary, null, 1 AS Sort_Order\r\n          FROM Master_Employee\r\n        UNION\r\n        SELECT null,\r\n               null,\r\n               'Total',\r\n               NVL(SUM(Salary), 0),\r\n               null,\r\n               2 AS Sort_Order\r\n          FROM Master_Employee)\r\n ORDER BY Department, Sno, Sort_Order;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SELECT Sno, Name, Department, Salary, SUM(Salary) over(PARTITION BY Department ORDER BY Sno) AS Cum_Salary FROM Master_Employee ORDER BY Department; How this Works PARTITION BY Department ORDER BY Sno Partition By will first Partition by Department.Which means the Cumulative Sum will get Reset once the Department Changes. ORDER BY Sno will make sure the Cumulative value&hellip; <a href=\"https:\/\/codethataint.com\/blog\/oracle-total-of-column\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-1129","post","type-post","status-publish","format-standard","hentry","category-oracle-database-2"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1129"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1129\/revisions"}],"predecessor-version":[{"id":1137,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1129\/revisions\/1137"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}