{"id":1086,"date":"2011-06-06T09:16:05","date_gmt":"2011-06-06T03:16:05","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1086"},"modified":"2016-01-07T11:31:06","modified_gmt":"2016-01-07T05:31:06","slug":"db2-pivot-table-2","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1086","title":{"rendered":"DB2 Pivot table &#8211; 2"},"content":{"rendered":"<p>Few months back I had <a href=\"https:\/\/techsatwork.com\/blog\/?p=898\" target=\"_blank\">posted an example<\/a> of how to create a pivot table using sql. That was how to create multiple rows into one column with values separated by comas. Well, this one shows how to transpose rows into columns.<\/p>\n<p>Suppose I have a table : TYPE_COUNT<\/p>\n<p>TYPE \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0COUNT<br \/>\n&#8212;&#8212; \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 &#8212;&#8212;&#8212;<br \/>\nABC \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a010<br \/>\nBCD \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a020<br \/>\nCDE \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a015<\/p>\n<p>and I want the results to look like this<\/p>\n<p>ABC \u00c2\u00a0 \u00c2\u00a0BCD \u00c2\u00a0 \u00c2\u00a0CDE<br \/>\n&#8212;&#8211; \u00c2\u00a0 \u00c2\u00a0&#8212;&#8211; \u00c2\u00a0 \u00c2\u00a0 &#8212;&#8211;<br \/>\n10 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 20 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 15<\/p>\n<p>Here the value on the first column is now represented as column name. \u00c2\u00a0 This can be accomplished by using the <a href=\"http:\/\/publib.boulder.ibm.com\/infocenter\/db2luw\/v9r7\/topic\/com.ibm.db2.luw.sql.ref.doc\/doc\/r0052622.html\" target=\"_blank\">DECODE<\/a> function . The below sql will do the job:<\/p>\n<p>SELECT<br \/>\nMAX(DECODE(type,&#8217;ABC&#8217;,count) AS ABC,<br \/>\nMAX(DECODE(type,&#8217;BCD&#8217;,count) AS BCD,<br \/>\nMAX(DECODE(type,&#8217;CDE&#8217;,count) AS CDE<br \/>\nFROM type_count;<\/p>\n<p>The same can be done using a combination of MAX and CASE function, but then it take lot more typing .<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Few months back I had posted an example of how to create a pivot table using sql. That was how to create multiple rows into one column with values separated by comas. Well, this one shows how to transpose rows into columns. Suppose I have a table : TYPE_COUNT TYPE \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0 \u00c2\u00a0COUNT [&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":[303,3,358],"tags":[615,491,617,486,616],"class_list":["post-1086","post","type-post","status-publish","format-standard","hentry","category-database","category-db2","category-how-to","tag-convert-rows-to-columns","tag-db2-pivot-table","tag-decode","tag-pivot-table","tag-transpose-rows-to-columns"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1086","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=1086"}],"version-history":[{"count":2,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1086\/revisions"}],"predecessor-version":[{"id":1088,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1086\/revisions\/1088"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}