Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > Database

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 11-04-2009, 04:42 PM
stoneheart stoneheart is offline
Registered User
 
Join Date: Nov 2009
Posts: 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.
Reply With Quote
  #2  
Old 11-05-2009, 08:47 AM
Hack's Avatar
Hack Hack is offline
Super Moderator
 
Join Date: Apr 2007
Location: Sterling Heights, Michigan
Posts: 7,719
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
Reply With Quote
  #3  
Old 11-06-2009, 12:20 PM
glafmitzpery glafmitzpery is offline
Registered User
 
Join Date: Oct 2009
Posts: 5
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
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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


All times are GMT -4. The time now is 08:04 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.