Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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
 
Old March 23rd, 2007, 02:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default 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
======================================
 
Old March 23rd, 2007, 07:32 AM
Authorized User
 
Join Date: Dec 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old March 24th, 2007, 12:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting records in a query firefighter2045 Access VBA 5 August 28th, 2008 08:14 AM
Create Related Records twsinc VB How-To 2 October 31st, 2006 06:51 PM
Viewing Multiple Related Records psudireddy Access 1 August 1st, 2006 11:07 PM
selecting records from excel androoo Pro VB 6 1 January 12th, 2005 12:18 PM
insert related records Haroldd Classic ASP Professional 2 April 15th, 2004 02:10 PM





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