|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Convert Record Set to View - SQL Server 2005
I'm hoping someone can point me to the right direction for this. I want to be able to convert my existing query which returns multiple records into a view that would look sort of like a spreadsheet.
My query that returns something like this: 1 Number_of_Bolts 11 2 Number_of_Nails 325 3 Number_of_Pins 1355 The query itself is: select question_id, question_name, question_value from question_data d inner join question_dictionary i on d.question_id = i.question_id where master_id = '2111' Given this, how do I get it into a format where all the records are on the same row spreadsheet style and the key identifier like Number_of_Bolts is a cell header with 11 as the value underneath it? I want this be a view in SQL Server. Thanks for any hints you all can provide. |
|
#2
|
||||
|
||||
|
Welcome to DevX
![]() Hope these help... http://technet.microsoft.com/en-us/l.../ms187956.aspx http://www.codeproject.com/KB/database/sqlviews.aspx
__________________
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section. ![]() Please use [Code]your code goes in here[/Code] tags when posting code. Before posting your question, did you look here? Got a question on Linux? Visit our Linux sister site. Modifications Required For VB6 Apps To Work On Vista ![]() Microsoft MVP 2005/2006/2007/2008/2009 |
|
#3
|
|||
|
|||
|
re: convert recordset to view
not sure if u need help on creating the view or how to get the data into a single row. if it's the latter:
sql server 2005 introduced the PIVOT operator. the unfortunate part is that with "pivot" you lose the "dynamic" aspects of a question table. you need to know in advance the "question_name" values and hard-code them into the pivot. here is your initial data set: select 'Number_of_Bolts' Question_Name, 11 Question_Value union select 'Number_of_Nails', 325 union select 'Number_of_Pins', 1355 here is the pivot query (replace the inner SQL "PivotSource" with your SQL that gets the list of questions): SELECT [Number_of_Bolts] , [Number_of_Nails] , [Number_of_Pins] FROM (select 'Number_of_Bolts' Question_Name, 11 Question_Value union select 'Number_of_Nails', 325 union select 'Number_of_Pins', 1355) As PivotSource PIVOT (Min (Question_Value) for Question_Name in ([Number_of_Bolts], [Number_of_Nails], [Number_of_Pins]) ) As p1; Resultset: Number_of_Bolts Number_of_Nails Number_of_Pins --------------- --------------- -------------- 11 325 1355 (1 row(s) affected) Last edited by glafmitzpery; 11-06-2009 at 12:22 PM. Reason: clarification |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Stored Procedure unable to print using sum with case statement | mahalirajesh | Database | 2 | 05-13-2008 11:10 AM |
| SQL Server 2005 — Move Data to Another Machine? | bgibson | Database | 2 | 01-14-2008 01:45 PM |
| ASP.NEt & SQL Server 2005 security, access question | SeQuell | ASP.NET | 2 | 09-25-2006 07:28 PM |
| NullPointerException when reading text file | Andrew McLellan | Java | 3 | 05-09-2001 06:34 PM |
| Access to SQL server | Nate | Database | 29 | 05-09-2001 11:04 AM |