﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server Newbies  / Transpose/Pivot Help / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 02:31:36 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>Thanks for the feedback &amp; the articles.</description><pubDate>Sun, 24 Feb 2013 11:33:09 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>[quote][b]ajames 36824 (2/22/2013)[/b][hr][quote][b]Jeff Moden (2/22/2013)[/b][hr][quote][b]ajames 36824 (2/22/2013)[/b][hr]You ROCK! :-D[/quote]Do you understand how and why it works?[/quote]Well it looks like it says that for each question in the QuestionID column, return the maximum numerical value related for the associated Response and making the rest blank. In order to do this, Response must be converted to characters. The Group By statement pivots all the other variables not in the statement and groups them based on those variables. Is that right?[/quote]Mostly correct. I say "mostly" because you don't actually need to convert the respose to VARCHAR.  Dwaine did that just so he could show a blank instead of a NULL (which also suppresses the NULL aggregate warning messages).If you'd like to do a deeper dive on CrossTabs and Pivots, here are a couple of articles that start you on the ground floor and move you all the way through dynamic CrossTabs.[url]http://www.sqlservercentral.com/articles/T-SQL/63681/[/url][url]http://www.sqlservercentral.com/articles/Crosstab/65048/[/url]</description><pubDate>Fri, 22 Feb 2013 20:33:34 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>[quote][b]ajames 36824 (2/22/2013)[/b][hr][quote][b]Jeff Moden (2/22/2013)[/b][hr][quote][b]ajames 36824 (2/22/2013)[/b][hr]You ROCK! :-D[/quote]Do you understand how and why it works?[/quote]Well it looks like it says that for each question in the QuestionID column, return the maximum numerical value related for the associated Response and making the rest blank. In order to do this, Response must be converted to characters. The Group By statement pivots all the other variables not in the statement and groups them based on those variables. Is that right?[/quote]Not sure I'd say it quite like that.The GROUP BY collapses all rows where the value of the grouped columns is the same, thus the rest of the columns are free to be pivoted.  Which is done by one MAX/CASE per column to be pivoted.</description><pubDate>Fri, 22 Feb 2013 19:18:20 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>[quote][b]Jeff Moden (2/22/2013)[/b][hr][quote][b]ajames 36824 (2/22/2013)[/b][hr]You ROCK! :-D[/quote]Do you understand how and why it works?[/quote]Well it looks like it says that for each question in the QuestionID column, return the maximum numerical value related for the associated Response and making the rest blank. In order to do this, Response must be converted to characters. The Group By statement pivots all the other variables not in the statement and groups them based on those variables. Is that right?</description><pubDate>Fri, 22 Feb 2013 13:51:37 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>[quote][b]ajames 36824 (2/22/2013)[/b][hr]You ROCK! :-D[/quote]Do you understand how and why it works?</description><pubDate>Fri, 22 Feb 2013 12:18:49 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>You ROCK! :-D</description><pubDate>Fri, 22 Feb 2013 12:04:55 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>Clarity is a beautiful thing.[code="sql"]SELECT ClientID, DOV, Form    ,[Alcohol1-001]=MAX(CASE QuestionID WHEN 'Alcohol1-001' THEN CAST(Response AS VARCHAR(3)) ELSE '' END )    ,[Alcohol1-002]=MAX(CASE QuestionID WHEN 'Alcohol2-002' THEN CAST(Response AS VARCHAR(3)) ELSE '' END )FROM @Imbibing GROUP BY ClientID, DOV, Form[/code]</description><pubDate>Fri, 22 Feb 2013 09:10:57 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>Thanks for cleaning that up. And that's right, you did say that bmp should be the desired results. Here you go.</description><pubDate>Fri, 22 Feb 2013 06:42:51 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>You're definitely on the right track to give us what we need to help you and reading the forum etiquette article was a smart thing to do.You didn't provide the DDL I asked for but I found that the DDL I used was good enough, so after changing the double quotes to single quotes and removing the last UNION ALL, this will run for me:[code="sql"]DECLARE @Imbibing TABLE    (ClientID INT, DOV DATE, Form VARCHAR(10), QuestionID VARCHAR(20), Response INT)INSERT INTO @ImbibingSELECT '7','2012-03-01','Audit','Alcohol1-001','0' UNION ALLSELECT '7','2012-03-01','Audit','Alcohol1-001','0' UNION ALLSELECT '8','2011-08-09','Audit','Alcohol1-001','0' UNION ALLSELECT '8','2011-08-09','Audit','Alcohol2-002','0' UNION ALLSELECT '8','2012-01-11','Audit','Alcohol1-001','0' UNION ALLSELECT '8','2012-01-11','Audit','Alcohol2-002','0' UNION ALLSELECT '9','2011-08-24','Audit','Alcohol1-001','0' UNION ALLSELECT '9','2011-08-24','Audit','Alcohol2-002','0' UNION ALLSELECT '9','2012-02-18','Audit','Alcohol1-001','1' UNION ALLSELECT '9','2012-02-18','Audit','Alcohol2-002','0' UNION ALLSELECT '12','2012-02-14','Audit','Alcohol1-001','0' UNION ALLSELECT '12','2012-02-14','Audit','Alcohol2-002','0' UNION ALLSELECT '13','2011-09-28','Audit','Alcohol1-001','0' UNION ALLSELECT '43','2011-11-01','Audit','Alcohol1-001','0' UNION ALLSELECT '44','2011-11-21','Audit','Alcohol1-001','0' UNION ALLSELECT '157','2012-02-29','Audit','Alcohol1-001','0' UNION ALLSELECT '157','2012-06-20','Audit','Alcohol1-001','0' UNION ALLSELECT '158','2012-03-21','Audit','Alcohol1-001','0' UNION ALLSELECT '158','2012-07-11','Audit','Alcohol1-001','1' UNION ALLSELECT '160','2012-06-19','Audit','Alcohol1-001','0' UNION ALLSELECT '160','2012-11-09','Audit','Alcohol1-001','0' UNION ALLSELECT '164','2012-07-17','Audit','Alcohol1-001','0' UNION ALLSELECT '174','2012-06-06','Audit','Alcohol1-001','1' UNION ALLSELECT '178','2012-07-17','Audit','Alcohol1-001','0' SELECT * FROM @Imbibing[/code]Now here is the issue.  The BMP you provided shows the SELECT statements above that I've inserted into the temporary table.  What it should show is the exact expected results based on the sample data you provided.</description><pubDate>Thu, 21 Feb 2013 17:31:23 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>Okay, I did this both ways in the hopes that one would be correct. I have attached bitmap file &amp; I created a query using the instructions in the "Forum Etiquette" to produce the data belowSELECT "7","2012-03-01","Audit","Alcohol1-001","0" UNION ALLSELECT "7","2012-03-01","Audit","Alcohol1-001","0" UNION ALLSELECT "8","2011-08-09","Audit","Alcohol1-001","0" UNION ALLSELECT "8","2011-08-09","Audit","Alcohol2-002","0" UNION ALLSELECT "8","2012-01-11","Audit","Alcohol1-001","0" UNION ALLSELECT "8","2012-01-11","Audit","Alcohol2-002","0" UNION ALLSELECT "9","2011-08-24","Audit","Alcohol1-001","0" UNION ALLSELECT "9","2011-08-24","Audit","Alcohol2-002","0" UNION ALLSELECT "9","2012-02-18","Audit","Alcohol1-001","1" UNION ALLSELECT "9","2012-02-18","Audit","Alcohol2-002","0" UNION ALLSELECT "12","2012-02-14","Audit","Alcohol1-001","0" UNION ALLSELECT "12","2012-02-14","Audit","Alcohol2-002","0" UNION ALLSELECT "13","2011-09-28","Audit","Alcohol1-001","0" UNION ALLSELECT "43","2011-11-01","Audit","Alcohol1-001","0" UNION ALLSELECT "44","2011-11-21","Audit","Alcohol1-001","0" UNION ALLSELECT "157","2012-02-29","Audit","Alcohol1-001","0" UNION ALLSELECT "157","2012-06-20","Audit","Alcohol1-001","0" UNION ALLSELECT "158","2012-03-21","Audit","Alcohol1-001","0" UNION ALLSELECT "158","2012-07-11","Audit","Alcohol1-001","1" UNION ALLSELECT "160","2012-06-19","Audit","Alcohol1-001","0" UNION ALLSELECT "160","2012-11-09","Audit","Alcohol1-001","0" UNION ALLSELECT "164","2012-07-17","Audit","Alcohol1-001","0" UNION ALLSELECT "174","2012-06-06","Audit","Alcohol1-001","1" UNION ALLSELECT "178","2012-07-17","Audit","Alcohol1-001","0" UNION ALL</description><pubDate>Thu, 21 Feb 2013 12:45:21 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>[quote][b]ajames 36824 (2/20/2013)[/b][hr]Allow me to try again. The original file comes from an excel spreadsheet that I pull into SPSS &amp; then export to a table in SQL. Then I wanted to write query to to pivot the necessary columns. Attached is the original data in excel &amp; a spreadsheet with the 1st few cases pivoted.[/quote]I think you'll find many people (including myself) will be reluctant to open Excel spreadsheet attachments due to the potential for viruses.You should post consumable sample data as I did and your expected results (perhaps as an attached bitmap).</description><pubDate>Wed, 20 Feb 2013 20:15:49 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>Allow me to try again. The original file comes from an excel spreadsheet that I pull into SPSS &amp; then export to a table in SQL. Then I wanted to write query to to pivot the necessary columns. Attached is the original data in excel &amp; a spreadsheet with the 1st few cases pivoted.</description><pubDate>Wed, 20 Feb 2013 19:38:33 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item><item><title>RE: Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>In the future, my suggestion is that you try to help your helpers a bit by providing DDL and consumable sample data like this:[code="sql"]DECLARE @Imbibing TABLE    (ClientID INT, DOV DATE, Form VARCHAR(10), QuestionID VARCHAR(20), Response INT)INSERT INTO @Imbibing SELECT 7, '3/1/2012','Audit','Alcohol1-001',0 UNION ALL SELECT 8, '8/9/2011','Audit','Alcohol1-001',0 UNION ALL SELECT 8, '1/11/2012','Audit','Alcohol1-001',0 UNION ALL SELECT 9, '8/24/2011','Audit','Alcohol1-002',0 UNION ALL SELECT 9, '2/18/2012','Audit','Alcohol1-002',1[/code]Remember that we're all volunteers here so this kindly helps us to respond quicker, better and with tested results.Now onto a proposed solution:[code="sql"]SELECT ClientID, DOV, Form    ,[Alcohol1-001]=CASE QuestionID WHEN 'Alcohol1-001' THEN 1 ELSE 0 END     ,[Alcohol1-002]=CASE QuestionID WHEN 'Alcohol1-002' THEN 1 ELSE 0 END FROM @Imbibing [/code]Mind you this is only a guess, because in your "desired results" you show 5 column headers but only 4 data columns.  And one of the Alcohol1-002 results rows doesn't match what I'm thinking you probably want.</description><pubDate>Wed, 20 Feb 2013 18:24:12 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>Transpose/Pivot Help</title><link>http://www.sqlservercentral.com/Forums/Topic1422316-1291-1.aspx</link><description>Hi. I have a table &amp; I need to transpose/pivot some of the columns into rows. The column that needs to be pivoted is related to another in that the Response column contains responses to the questions in the QuestionID column. I need to transpose/pivot the QuestionID column but keep the corresponding responses in their proper place. Below is an example that I did by hand. I searched the forum and there were several examples similar to what I'm looking for, but not quite close enough.										Current format					ClientID	DOV	        Form	   QuestionID	  Response	7	        3/1/2012	Audit	   Alcohol1-001	  0	8	        8/9/2011	Audit	   Alcohol1-001	  0	8	        1/11/2012	Audit	   Alcohol1-001	  0	9	        8/24/2011	Audit	   Alcohol1-002	  0	9	        2/18/2012	Audit	   Alcohol1-002	  1											Desired format					ClientID	DOV	        Form	   Alcohol1-001	Alcohol1-002	7	        3/1/2012	Audit	   0		8	        8/9/2011	Audit	   0		8	        1/11/2012	Audit	   0		9	        8/24/2011	Audit	   0		9	        2/18/2012	Audit		                 1											Is there any way to do this? Any help would be greatly appreciated.					</description><pubDate>Wed, 20 Feb 2013 14:46:04 GMT</pubDate><dc:creator>ajames 36824</dc:creator></item></channel></rss>