SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Transpose/Pivot Help


Transpose/Pivot Help

Author
Message
ajames 36824
ajames 36824
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 18
Hi. I have a table & 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.
dwain.c
dwain.c
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21677 Visits: 6431
In the future, my suggestion is that you try to help your helpers a bit by providing DDL and consumable sample data like this:


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




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:


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




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.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ajames 36824
ajames 36824
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 18
Allow me to try again. The original file comes from an excel spreadsheet that I pull into SPSS & 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 & a spreadsheet with the 1st few cases pivoted.
Attachments
Test data, 2013.02.20.xlsx (22 views, 9.00 KB)
Test data - rev, 2013.02.20.xlsx (18 views, 7.00 KB)
dwain.c
dwain.c
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21677 Visits: 6431
ajames 36824 (2/20/2013)
Allow me to try again. The original file comes from an excel spreadsheet that I pull into SPSS & 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 & a spreadsheet with the 1st few cases pivoted.


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).


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ajames 36824
ajames 36824
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 18
Okay, I did this both ways in the hopes that one would be correct. I have attached bitmap file & I created a query using the instructions in the "Forum Etiquette" to produce the data below

SELECT "7","2012-03-01","Audit","Alcohol1-001","0" UNION ALL
SELECT "7","2012-03-01","Audit","Alcohol1-001","0" UNION ALL
SELECT "8","2011-08-09","Audit","Alcohol1-001","0" UNION ALL
SELECT "8","2011-08-09","Audit","Alcohol2-002","0" UNION ALL
SELECT "8","2012-01-11","Audit","Alcohol1-001","0" UNION ALL
SELECT "8","2012-01-11","Audit","Alcohol2-002","0" UNION ALL
SELECT "9","2011-08-24","Audit","Alcohol1-001","0" UNION ALL
SELECT "9","2011-08-24","Audit","Alcohol2-002","0" UNION ALL
SELECT "9","2012-02-18","Audit","Alcohol1-001","1" UNION ALL
SELECT "9","2012-02-18","Audit","Alcohol2-002","0" UNION ALL
SELECT "12","2012-02-14","Audit","Alcohol1-001","0" UNION ALL
SELECT "12","2012-02-14","Audit","Alcohol2-002","0" UNION ALL
SELECT "13","2011-09-28","Audit","Alcohol1-001","0" UNION ALL
SELECT "43","2011-11-01","Audit","Alcohol1-001","0" UNION ALL
SELECT "44","2011-11-21","Audit","Alcohol1-001","0" UNION ALL
SELECT "157","2012-02-29","Audit","Alcohol1-001","0" UNION ALL
SELECT "157","2012-06-20","Audit","Alcohol1-001","0" UNION ALL
SELECT "158","2012-03-21","Audit","Alcohol1-001","0" UNION ALL
SELECT "158","2012-07-11","Audit","Alcohol1-001","1" UNION ALL
SELECT "160","2012-06-19","Audit","Alcohol1-001","0" UNION ALL
SELECT "160","2012-11-09","Audit","Alcohol1-001","0" UNION ALL
SELECT "164","2012-07-17","Audit","Alcohol1-001","0" UNION ALL
SELECT "174","2012-06-06","Audit","Alcohol1-001","1" UNION ALL
SELECT "178","2012-07-17","Audit","Alcohol1-001","0" UNION ALL
Attachments
Pivot Data.bmp (6 views, 464.00 KB)
dwain.c
dwain.c
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21677 Visits: 6431
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:


DECLARE @Imbibing TABLE
(ClientID INT, DOV DATE, Form VARCHAR(10), QuestionID VARCHAR(20), Response INT)

INSERT INTO @Imbibing
SELECT '7','2012-03-01','Audit','Alcohol1-001','0' UNION ALL
SELECT '7','2012-03-01','Audit','Alcohol1-001','0' UNION ALL
SELECT '8','2011-08-09','Audit','Alcohol1-001','0' UNION ALL
SELECT '8','2011-08-09','Audit','Alcohol2-002','0' UNION ALL
SELECT '8','2012-01-11','Audit','Alcohol1-001','0' UNION ALL
SELECT '8','2012-01-11','Audit','Alcohol2-002','0' UNION ALL
SELECT '9','2011-08-24','Audit','Alcohol1-001','0' UNION ALL
SELECT '9','2011-08-24','Audit','Alcohol2-002','0' UNION ALL
SELECT '9','2012-02-18','Audit','Alcohol1-001','1' UNION ALL
SELECT '9','2012-02-18','Audit','Alcohol2-002','0' UNION ALL
SELECT '12','2012-02-14','Audit','Alcohol1-001','0' UNION ALL
SELECT '12','2012-02-14','Audit','Alcohol2-002','0' UNION ALL
SELECT '13','2011-09-28','Audit','Alcohol1-001','0' UNION ALL
SELECT '43','2011-11-01','Audit','Alcohol1-001','0' UNION ALL
SELECT '44','2011-11-21','Audit','Alcohol1-001','0' UNION ALL
SELECT '157','2012-02-29','Audit','Alcohol1-001','0' UNION ALL
SELECT '157','2012-06-20','Audit','Alcohol1-001','0' UNION ALL
SELECT '158','2012-03-21','Audit','Alcohol1-001','0' UNION ALL
SELECT '158','2012-07-11','Audit','Alcohol1-001','1' UNION ALL
SELECT '160','2012-06-19','Audit','Alcohol1-001','0' UNION ALL
SELECT '160','2012-11-09','Audit','Alcohol1-001','0' UNION ALL
SELECT '164','2012-07-17','Audit','Alcohol1-001','0' UNION ALL
SELECT '174','2012-06-06','Audit','Alcohol1-001','1' UNION ALL
SELECT '178','2012-07-17','Audit','Alcohol1-001','0'

SELECT * FROM @Imbibing




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.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ajames 36824
ajames 36824
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 18
Thanks for cleaning that up. And that's right, you did say that bmp should be the desired results. Here you go.
Attachments
Test data - rev.bmp (11 views, 464.00 KB)
dwain.c
dwain.c
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21677 Visits: 6431
Clarity is a beautiful thing.


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





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
ajames 36824
ajames 36824
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 18
You ROCK! :-D
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (260K reputation)SSC Guru (260K reputation)SSC Guru (260K reputation)SSC Guru (260K reputation)SSC Guru (260K reputation)SSC Guru (260K reputation)SSC Guru (260K reputation)SSC Guru (260K reputation)

Group: General Forum Members
Points: 260693 Visits: 42163
ajames 36824 (2/22/2013)
You ROCK! :-D


Do you understand how and why it works?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search