Question : I only want one record from my left join call

T-SQL 2000

I have 4 tables, the last 3 are all left joins. In several unique cases the last LEFT JOIN is pulling back 2-3 records. This results is me having multiple records that are identicle save for the one column that I am getting from that last table.

I know I'm missing the obvious, but what is the best way to go about fixing this. The actual data I pull from that last table isn't even very important, so as long as I get only one of the possible 2-3 results, I'm happy.

If it's needed I can post a cut down version of the query.

Thanks

Answer : I only want one record from my left join call

Imagine the tables called A, B, C and D. In your case, it's the 'D' table that has a 1-n relation with table C, so that it might return > 1 record to you.

Here's an example of a possible query you may have:

SELECT A.ID, A.AField, B.BField, C.CField, D.DField
FROM
  ((A LEFT JOIN B ON A.ID = B.ID) LEFT JOIN C ON B.ID = C.ID) LEFT JOIN D ON C.ID = D.ID

If this results multiple values for D.DField, where all the other fields are the same, this does solve your question:
SELECT A.ID, A.AField, B.BField, C.CField, MIN(D.DField)
FROM
  ((A LEFT JOIN B ON A.ID = B.ID) LEFT JOIN C ON B.ID = C.ID) LEFT JOIN D ON C.ID = D.ID
GROUP BY A.ID, A.AField, B.BField, C.CField

This returns all rows with a different set of A.ID, A.AField, B.BField, C.CField. Because D.DField differs, you must have an aggregate function on D.DField to tell the database what to do with D.DField, if it finds an identical set. In this example, it returns the minimum value.
Example, imagine the first query to return the following:
ID = 1, AField = 'bla', BField = 'blabla', CField = 'mooh', DField = 'smile'
ID = 1, AField = 'bla', BField = 'blabla', CField = 'mooh', DField = 'cry'
ID = 2, AField = 'aaa', BField = 'aaaaaa', CField = 'honk', DField = 'laugh'

The group by will return two sets:
ID = 1, AField = 'bla', BField = 'blabla', CField = 'mooh' and
ID = 2, AField = 'aaa', BField = 'aaaaaa', CField = 'honk', only for the first set it finds two values for D.DField, namely 'smile' and 'cry'. You need to tell the database what to do with these two values: which one of them to add as D.DField to your set.
Examples: Min(D.DField) tells the database to return the minimum (in this case alphabetically), so it will return the following recordset:
ID = 1, AField = 'bla', BField = 'blabla', CField = 'mooh', DField = 'cry'
ID = 2, AField = 'aaa', BField = 'aaaaaa', CField = 'honk', DField = 'laugh'

Max(D.DField) returns:
ID = 1, AField = 'bla', BField = 'blabla', CField = 'mooh', DField = 'smile'
ID = 2, AField = 'aaa', BField = 'aaaaaa', CField = 'honk', DField = 'laugh'

Count(D.DField) returns:
ID = 1, AField = 'bla', BField = 'blabla', CField = 'mooh', DField = 2
ID = 2, AField = 'aaa', BField = 'aaaaaa', CField = 'honk', DField = 'laugh'

You need to choose an aggregate function that suits you best.

Good luck and I hope this helps. If you need help on your actual query, let me know.

Cheers, Luc
Random Solutions  
 
programming4us programming4us