Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 2nd, 2007, 04:35 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Results in line from sam table

Hi All,
I wish to build a query that will return a resultset compiling records from the same table.

The table is something like this
tblTestData
ID GroupID FieldID FieldValue
1 1 1 'test11'
1 1 2 'test12'
1 1 3 'test13'
1 2 1 'test21'
1 2 2 'test22'
1 2 3 'test23'
1 3 1 'test31'
1 3 2 'test32'
1 3 3 'test33'

I wish to be able to create a SP to return the fields in one record for each "Group"

So the result set would be
ID Group Field1 Field2 Field3
1 1 'test11' 'test12' 'test13'
1 2 'test21' 'test22' 'test23'
1 3 'test31' 'test32' 'test33'

I had hoped to provide the paramters ID,Field1ID, Field2ID, Field3ID to obtain this result set but can not get the relationship correct without doing what seems to be a huge volume of sub queries.

Can anyone point me in the right direction as to what type of constuct/pattern I should be using for this type of query.


Many thanks in advance for any assistance.




======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
Reply With Quote
  #2 (permalink)  
Old July 4th, 2007, 11:44 AM
Registered User
 
Join Date: Jul 2007
Location: , , USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you have to create a temporary table in the procedure with the structure that you want and fill this table for each group selected
this temporary table should contain these fields

ID Group Field1 Field2 Field3

the select from the initial table filtering on the id the loop over the cursor and fill the temporary table , then select from temp. table and return the result.
note: temp. table will be deleted automatically win the scope of the procedure is finished.
Reply With Quote
  #3 (permalink)  
Old August 2nd, 2007, 04:57 AM
Registered User
 
Join Date: Aug 2007
Location: Bangalore, Bangalore, India.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi rodmcleay,
:)
hope u r fine. I went thru ur question and u an do it without using temporary tables. the query can be written as given below

Select A.Id as Id ,
A.GroupId as GroupId ,
A.Field1 as Field1,
A.Field2 as Field2,
A.Field3 as Field3
From
(
Select Id,
GroupId ,
Case when FieldId=1
Then FieldValue as Field1,
Case when FieldId=2
Then FieldValue as Field2,
Case when FieldId=3
Then FieldValue as Field3
From tblTestData
)A

Do try it out and lemme know if it doesnt work..

Thanks and Regards,
Anju Renjith
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems rendering to a table with sorted results jlmacdonald XSLT 6 March 27th, 2008 05:54 PM
Transferring results from a query to another table GujuNilesh SQL Server 2005 1 March 2nd, 2007 02:00 PM
Chapter 8: Results Table not building Steve Rosenthal BOOK: Beginning Access VBA 1 September 7th, 2004 06:11 AM
How to store results of several scripts in table fawadr4656abp Oracle 0 June 13th, 2003 03:00 AM



All times are GMT -4. The time now is 05:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.