 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 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
|
|
|
|

March 23rd, 2007, 02:14 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Selecting one of possibly many related records
Hi all,
I have a situation where there is a one to many relation ship between two tables and I need to select only one of the records in the second table depending on the existance of particular value in a particular field.
For example
TableA: ID
TableB: ID, TableAID, Field1, Field2
Table A
1
2
3
Table B
1,1,value1,value3
2,1,value2,value3
3,2,value2,value3
desired results
1,1,value1,value3
3,2,value2,value3
null,3,null,null
[I edited the results from my original post as they where incorrect, please ignor results in email.]
The 2,1,value2,value3 record is ommited because there is a value1 available.
The TableAID = 3 still has to be returned as a right outer join
If there is a record for the TableAID that has value1 in the Field1 column then I need to select that record, as in TableAID=1
However if there is not a value1 in the table for Field1 I need to select the value2 record, as in TableAID=2
But still only returning one record per TableAID.
This is far less complex than the query i have to work with but I think it illustates the problem.
Any Assistance will be grately appreciated.
======================================
"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
======================================
|
|

March 23rd, 2007, 07:32 AM
|
|
Authorized User
|
|
Join Date: Dec 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I made some assumptions due to other possible requirements that you didn't supply or where in error.
Are the field1 values really just two possible values?
Resultset??
id tableaid field1 field2
1 1 value1 value3
2 2 value2 value3
3 NULL NULL NULL
HTH
<code>
DECLARE @tablea TABLE (id int)
DECLARE @tableb TABLE (id int, tableaid int, field1 varchar(10), field2 varchar(10))
INSERT INTO @tablea SELECT 1
INSERT INTO @tablea SELECT 2
INSERT INTO @tablea SELECT 3
INSERT INTO @tableb SELECT 1,1,'value1','value3'
INSERT INTO @tableb SELECT 2,1,'value2','value3'
INSERT INTO @tableb SELECT 3,2,'value2','value3'
SELECT
a.id
, CASE WHEN b.field1 IS NOT NULL THEN b.tableaid ELSE c.tableaid END
, ISNULL(b.field1, c.field1)
, CASE WHEN b.field1 IS NOT NULL THEN b.field2 ELSE c.field2 END
FROM @tablea a
LEFT JOIN (SELECT * FROM @tableb WHERE field1 = 'value1') b ON a.id = b.tableaid
LEFT JOIN (SELECT * FROM @tableb WHERE field1 = 'value2') c ON a.id = c.tableaid
</code>
Adam Gossage
Lake Wylie, SC, USA
|
|

March 24th, 2007, 12:49 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Hi Adam,
Thanks very much for your time.
Yes, your version of the resultset is correct, thankyou and my appologies for not being clear.
There can be more than two values for field1 but only two of the values are of concern for this query.
Your solutions looks great, thanks very much for that.
Now I have the task of putting this into my real situation.
Which I believe I can do thanks to your 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
======================================
|
|
 |