Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 2nd, 2007, 04:35 AM
Friend of Wrox
Join Date: Jun 2003
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
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."
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
Old July 4th, 2007, 11:44 AM
Registered User
Join Date: Jul 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

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.
Old August 2nd, 2007, 04:57 AM
Registered User
Join Date: Aug 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts

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

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

Thanks and Regards,
Anju Renjith

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

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