{"id":898,"date":"2010-12-17T11:05:52","date_gmt":"2010-12-17T05:05:52","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=898"},"modified":"2016-01-07T11:31:08","modified_gmt":"2016-01-07T05:31:08","slug":"db2-pivot-table","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=898","title":{"rendered":"DB2 Pivot Table &#8211; 1"},"content":{"rendered":"<p>A colleague of mine asked me how to convert values on multiple rows into a single column. \u00c2\u00a0The search took me to IT toolbox. \u00c2\u00a0Here is how you would do it:<br \/>\nSuppose \u00c2\u00a0I have a table with rows like this :<\/p>\n<p>NAME\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 SKILL<br \/>\n&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 db2<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 sql server<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0 oracle<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Db2 Z\/os<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 mysql<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0 php<br \/>\njames\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 php<br \/>\njames\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 perl<br \/>\njames\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 c++<br \/>\njames\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 c#<br \/>\nerik\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 ajax<br \/>\nerik\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 java<\/p>\n<p>and I want the result sets to look like this :<\/p>\n<p>Name\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Skills<\/p>\n<p>erik\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 ajax,java<br \/>\njames\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 php,perl,c++,c#<br \/>\nraju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 db2,sql server,oracle,Db2 Z\/os,mysql,php<\/p>\n<p>This sql builds a pivot table and uses XML functions to convert the rows to XML tags and then a simple REPLACE converts the tags to a comma and the substr function on the outer select cuts out the last comma :<\/p>\n<p>SELECT NAME,<br \/>\nSUBSTR(skill_list, 1, LENGTH(skill_list) -1) as &#8220;Skills&#8221;<br \/>\nFROM<br \/>\n(SELECT name,<br \/>\nREPLACE<br \/>\n(REPLACE<br \/>\n(XMLSERIALIZE<br \/>\n(CONTENT XMLAGG(XMLELEMENT(Name &#8220;A&#8221;, skill))<br \/>\nAS VARCHAR(120)),<br \/>\n&#8216;&lt;A&gt;&#8217;, &#8221;),<br \/>\n&#8216;&lt;\/A&gt;&#8217;, &#8216;,&#8217;) AS skill_list<br \/>\nFROM skills<br \/>\nGROUP BY name) AS X;<\/p>\n<p>Its pretty slick, this was adapted from a presentation by <a href=\"https:\/\/www.ibm.com\/developerworks\/mydeveloperworks\/blogs\/SQLTips4DB2LUW\/?lang=en\" target=\"_blank\">Serge Rileau<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A colleague of mine asked me how to convert values on multiple rows into a single column. \u00c2\u00a0The search took me to IT toolbox. \u00c2\u00a0Here is how you would do it: Suppose \u00c2\u00a0I have a table with rows like this : NAME\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 SKILL &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; raju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 db2 raju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 sql server raju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0 oracle raju\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 Db2 [&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,358],"tags":[489,943,492,491,486,487,490,488],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-db2","category-how-to","tag-convert-rows-into-columns","tag-db2","tag-db2-convert-multiple-rows-to-a-single-row","tag-db2-pivot-table","tag-pivot-table","tag-xml","tag-xml-elements","tag-xquery"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/898","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=898"}],"version-history":[{"count":6,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/898\/revisions"}],"predecessor-version":[{"id":1508,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/898\/revisions\/1508"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}