{"id":254,"date":"2010-04-30T09:37:59","date_gmt":"2010-04-30T09:37:59","guid":{"rendered":"http:\/\/www.thestupidbox.com\/blog\/?p=254"},"modified":"2021-06-25T04:16:57","modified_gmt":"2021-06-25T04:16:57","slug":"oracle-string-aggregation","status":"publish","type":"post","link":"http:\/\/thestupidbox.com\/blog\/oracle-string-aggregation\/","title":{"rendered":"Oracle String aggregation"},"content":{"rendered":"<p>This post is about string aggregation in oracle. Let&#8217;s say you have a table structure like below<\/p>\n<table class=\"wp-caption\" width=\"250\">\n<thead>\n<tr>\n<th>Country<\/th>\n<th>State<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>USA<\/td>\n<td>CA<\/td>\n<\/tr>\n<tr>\n<td>USA<\/td>\n<td>WA<\/td>\n<\/tr>\n<tr>\n<td>USA<\/td>\n<td>TX<\/td>\n<\/tr>\n<tr>\n<td>India<\/td>\n<td>Tamil Nadu<\/td>\n<\/tr>\n<tr>\n<td>India<\/td>\n<td>Kerala<\/td>\n<\/tr>\n<tr>\n<td>India<\/td>\n<td>Andhra Pradesh<\/td>\n<\/tr>\n<tr>\n<td>India<\/td>\n<td>Karnataka<\/td>\n<\/tr>\n<tr>\n<td>UK<\/td>\n<td>Essex<\/td>\n<\/tr>\n<tr>\n<td>UK<\/td>\n<td>Surrey<\/td>\n<\/tr>\n<tr>\n<td>UK<\/td>\n<td>Kent<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>And if you want to get an output like this<\/p>\n<table class=\"wp-caption\" width=\"250\">\n<thead>\n<tr>\n<th>Country<\/th>\n<th>States<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>USA<\/td>\n<td>CA,WA,TX<\/td>\n<\/tr>\n<tr>\n<td>India<\/td>\n<td>Tamil Nadu, Kerala, Andhra Pradesh, Karnataka<\/td>\n<\/tr>\n<tr>\n<td>UK<\/td>\n<td>Essex, Surrey, Kent<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are lot of ways you can achieve this. Like,<br \/>\n<strong>collect<\/strong><\/p>\n<pre>select country, CAST(COLLECT(states) AS number_ntt) AS States from table;<\/pre>\n<p> (supported from 10g)<\/p>\n<p>in the above query, number_ntt is a table with below definition<br \/>\nCREATE OR REPLACE TYPE varchar2_ntt AS TABLE OF VARCHAR2(4000);<\/p>\n<p>And you have to write your own function to convert the table structure to a string. <a href=\"http:\/\/www.oracle-developer.net\/display.php?id=306\" target=\"_new\" rel=\"noopener\">like this<\/a><\/p>\n<p><strong>stragg<\/strong><\/p>\n<pre>select country, stragg(states) from table;<\/pre>\n<p>this query is supported from only<\/p>\n<p><strong>listagg<\/strong><\/p>\n<pre>SELECT country, LISTAGG(states, ',') WITHIN GROUP (ORDER BY states) AS states\r\nFROM   table\r\nGROUP BY country;<\/pre>\n<p>this function also supported from 11g version only. But you have additional facility like, you can sort them.<\/p>\n<p>And there are few more techniques you can find them <a href=\"http:\/\/www.oracle-base.com\/articles\/misc\/StringAggregationTechniques.php\" target=\"_new\" rel=\"noopener\">here<\/a><\/p>\n<p><strong>XML<\/strong><\/p>\n<p>I read about one more nice method.. couldn&#8217;t remember the link :() which I think is the coolest of all&#8230; \ud83d\ude09<\/p>\n<pre>select xmlelement(\"country\", xmlattributes(country as \"country\"), xmlagg(xmlelement(\"state\",state))) from table\r\ngroup BY country;<\/pre>\n<p>Once you get your data in XML format, you can format it to your desired output. XSL is for that&#8230; \ud83d\ude42 you can either format in oracle itself using XMLTRANSFORM function or using any XSL provider.<\/p>\n<p>after writing above all, found a <a href=\"http:\/\/www.sqlsnippets.com\/en\/topic-11787.html\" target=\"_new\" rel=\"noopener\">very good article<\/a> of the same&#8230; I can&#8217;t blog anything if I have to write on my own. \ud83d\ude1b<\/p>\n<p>Happy querying!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about string aggregation in oracle. Let&#8217;s say you have a table structure like below Country State USA CA USA WA USA TX India Tamil Nadu India Kerala India Andhra Pradesh India Karnataka UK Essex UK Surrey UK&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[66,64,69,68,65,67],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-technical","tag-aggregate-function","tag-oracle","tag-oracle-sorted-aggregate","tag-oracle-sql","tag-string-aggregate","tag-xml-aggregate"],"_links":{"self":[{"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":13,"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":750,"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/posts\/254\/revisions\/750"}],"wp:attachment":[{"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/thestupidbox.com\/blog\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}