Solved

Turn multiple columns into rows

Posted on 2009-04-08
3
589 Views
Last Modified: 2012-05-06
I have a survey website that collects data and the answers to the questions are stored in a table so that each questionID and answer is in its own row in a database table.

What I want to do is kinda complicated and I KNOW I can do it but I am having a brain freeze currently.

I have the query below that returns a question and an answer from a table (the number of questions/answers is dynamic).  The query below only returns one persons questions/answers (for info - if I comment out the line "AND ID.ItemID = '160'" it will return ALL the questions/answers).

I uploaded a spreadsheet that has how it is coming from the query and how I want it.

How do I do this?  Then once I do it with this one persons answers to these questions how can I get it to work for ALL users (comment out this:AND ID.ItemID = '160')

I can use any sort of code in TSQL and a Stored procedure to do it, create a dynamic query, etc.  I looked at Pivot Tables but either do not understand them or it will not work for what I want it to do.

If you need more details/more examples let me know.

This is only for ONE persons answers to their questions,


Select * 
From (
Select cast(row_number() over (order by QT.DisplayOrder, QT.QuestionID) as varchar(5000)) + '. ' + QT.QuestionText as Question, 
	Case When PVL.PropertyDescription IS NULL THEN P.PropertyValue ELSE PVL.PropertyDescription END as QuestionAnswer
From Property P 
Inner Join ItemData ID ON P.ItemID = ID.ItemID
Inner Join QuestionTable QT ON QT.QuestionID = P.PropertyCode 
Left Outer Join PropertyValueLookup PVL ON PVL.PropertyCode = QT.PropertyCode AND 
	PVL.PropertyValueID = Case When ISNUMERIC(P.PropertyValue) = '1' Then P.PropertyValue Else PVL.PropertyValueID END 
Where QT.TypeID = '8'
AND ID.ItemID = '160'
--AND QT.QUestionID = '9'
 
) as R

Open in new window

Help.xls
0
Comment
Question by:brad2575
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
spprivate earned 500 total points
ID: 24100577
Here is a link which helped me do this.Basically you need to create a stored proc
and pass the parameters

http://p2p.wrox.com/sql-server-2000/24916-pivot-table.html
0
 
LVL 16

Author Comment

by:brad2575
ID: 24100639
This looks like it would work for a single table and a single value only.  I could alter it to work with my Join statement above but not sure about the multiple fields?

0
 
LVL 16

Author Closing Comment

by:brad2575
ID: 31568184
Gave you a C only because it was not what I was looking for but you still tried to help out.  I went with a SP and a temp tables.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question