{"id":402,"date":"2014-09-29T05:47:37","date_gmt":"2014-09-29T05:47:37","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=402"},"modified":"2014-09-29T05:47:37","modified_gmt":"2014-09-29T05:47:37","slug":"dynamic-pivot-table","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=402","title":{"rendered":"Dynamic Pivot Table"},"content":{"rendered":"<p>DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)<br \/>\nDECLARE @ColumnName AS NVARCHAR(MAX)<\/p>\n<p>&#8211;Get distinct values of the PIVOT Column<br \/>\nSELECT @ColumnName= ISNULL(@ColumnName + &#8216;,&#8217;,&#8221;)<br \/>\n       + QUOTENAME(tablecolumn)<br \/>\nFROM (SELECT DISTINCT tablecolumn FROM tableName) AS Courses<\/p>\n<p>&#8211;Prepare the PIVOT query using the dynamic<br \/>\nSET @DynamicPivotQuery =<br \/>\n  N&#8217;SELECT Year, &#8216; + @ColumnName + &#8216;<br \/>\n    FROM tableName<br \/>\n    PIVOT(SUM(ColumnName)<br \/>\n          FOR columnName IN (&#8216; + @ColumnName + &#8216;)) AS PVTTable&#8217;<br \/>\n&#8211;Execute the Dynamic Pivot Query<br \/>\nEXEC sp_executesql @DynamicPivotQuery<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) DECLARE @ColumnName AS NVARCHAR(MAX) &#8211;Get distinct values of the PIVOT Column SELECT @ColumnName= ISNULL(@ColumnName + &#8216;,&#8217;,&#8221;) + QUOTENAME(tablecolumn) FROM (SELECT DISTINCT tablecolumn FROM tableName) AS Courses &#8211;Prepare the PIVOT query using the dynamic SET @DynamicPivotQuery = N&#8217;SELECT Year, &#8216; + @ColumnName + &#8216; FROM tableName PIVOT(SUM(ColumnName) FOR columnName IN (&#8216; + @ColumnName + &#8216;)) AS PVTTable&#8217;&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=402\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-402","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=402"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/402\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}