{"id":17,"date":"2008-11-17T22:30:40","date_gmt":"2008-11-17T22:30:40","guid":{"rendered":"http:\/\/jcrawfor74.wordpress.com\/?p=17"},"modified":"2013-01-24T02:03:39","modified_gmt":"2013-01-23T15:03:39","slug":"sql-2005-unpivot-query","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2008\/11\/17\/sql-2005-unpivot-query\/","title":{"rendered":"SQL 2005 Unpivot Query"},"content":{"rendered":"<div id=\"ntsbl-1874895929\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><p>The other day I had need to use an unpivot query in SQL 2005. Thought it was really cool so here it is&#8230;<\/p>\n<p>Assume you have the following tables<\/p>\n<ul>\n<li>tblFolder: represents a folder, primary key ID.<\/li>\n<li>tblFolderItem: many to many join between folder and item<\/li>\n<li>tblItem: a table that&nbsp;represents an item. It has the following columns, id, date1, date2, date3.<\/li>\n<\/ul>\n<p>Therefore the folder can contain many items and each item&nbsp;has their own set of dates. What I want is the maximum date of all dates for all items in stored against each folder. So lets begin&#8230;<\/p>\n<p>The item has three different date fields that represent different dates that certain actions occurred. To start you want to get the maximum value for each date column.&nbsp; You write a query like this:<br \/>\n[sql]<br \/>\nselect<br \/>\n    f.id [folderId],<br \/>\n    max(i.date1) [maxDate1],<br \/>\n    max(i.date2) [maxDate2],<br \/>\n    max(i.date3) [maxDate3]<br \/>\nfrom tblFolder f<br \/>\n    inner join tblFolderItem fi on fi.folderId = f.id<br \/>\n    inner join tblItem i on fi.itemId = i.id<br \/>\ngroup by f.id<br \/>\n[\/sql]<\/p>\n<p>This will give you results like:<br \/>\n1, dateA, dateB, dateC<br \/>\n2, dateX, dateY, dateZ.<\/p>\n<p>But what you really want is the max of dateA\/B &amp; C for folder 1 and max of date X\/Y\/Z for folder 2.<\/p>\n<p>This is where unpivot comes in, adjust your query like this:<br \/>\n[sql]<br \/>\nselect<br \/>\n    folderId,<br \/>\n    Max(XXXX) as maxDate<br \/>\nfrom<br \/>\n(<br \/>\n    select<br \/>\n        f.id [folderId],<br \/>\n        max(i.date1) [maxDate1],<br \/>\n        max(i.date2) [maxDate2],<br \/>\n        max(i.date3) [maxDate3]<br \/>\n    from tblFolder f<br \/>\n        inner join tblFolderItem fi on fi.folderId = f.id<br \/>\n        inner join tblItem i on fi.itemId = i.id<br \/>\n    group by f.id<br \/>\n) AS tblDates<br \/>\nUNPIVOT<br \/>\n    (XXXX FOR id IN<br \/>\n        (maxDate1, maxDate2, maxDate3)<br \/>\n    )AS unpvt<br \/>\ngroup by folderId<br \/>\n[\/sql]<br \/>\nThe result of the unpivot query is to return a table of results like:<\/p>\n<p>id, MaxDate<br \/>\n1, dateA<br \/>\n1, dateB<br \/>\n1, dateC<br \/>\n2, dateX<br \/>\n2, dateY<br \/>\n2, dateZ<\/p>\n<p>now you do the group by on the id, and the Max on your column name (XXXX) and you are done.<\/p>\n<p>The XXXX can obviously be any name for a column you want, as long as it does not conflict with other columns in your base query.<\/p>\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day I had need to use an unpivot query in SQL 2005. Thought it was really cool so here it is&#8230; Assume you [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}