p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SQL Language (http://p2p.wrox.com/forumdisplay.php?f=100)
-   -   Results in line from sam table (http://p2p.wrox.com/showthread.php?t=59645)

rodmcleay July 2nd, 2007 04:35 AM

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

mkmustafa July 4th, 2007 11:44 AM

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.

anjurenjith August 2nd, 2007 04:57 AM

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..[V]

Thanks and Regards,
Anju Renjith

All times are GMT -4. The time now is 02:36 PM.

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