Simple SQL JOIN question?

  • Thread starter Thread starter kbc
  • Start date Start date

kbc

kbc

Associate
Joined
20 Nov 2004
Posts
1,629
Location
London
I have 2 tables, and all I want to do is always return all the rows from Table A regardless if there is a link to Table B via field Test No. But I will be passing the following filters:

Table A - Group No = ABC
Table B - Sample No = 123


Table A
Group No | Test No | Description
123 | 1 | First Test
123 | 2 | Second Test
123 | 3 | Third Test

Table B
Sample No | Test No | Result Description
ABC | 1 | Some Result
ABC | 3 | Some More Result

I would like the above to return something like:
Sample No | Test No | Description | Result Description
123 | 1 | First Test | Some Result
123 | 2 | Second Test |
123 | 3 | Third Test | Some More Result

I did a left outter join, but it did not return the second row above.
 
That works if I don't pass a filter to Table B.

Am I structuring the whole query wrong? As I need to pass a Sample No to Table B.

Not focusing! - Just needed a NULL in the condition
 
Last edited:
OK. I still couldn't get it working:

Table_1
[Group No] [Test No] [Description]
123 1 [First Test]
123 2 [Second Test]
123 3 [Third Test]

Table_2
[Sample No] [Test No] [Result Description]
ABC 1 [Some More Result]
ABC 3 [Some Result]
DEF 1 [A Result]
DEF 2 [Results More]
DEF 3 [Bad Results]

SELECT Table_1.[Group No], Table_1.[Test No], Table_1.Description, Table_2.[Result Description]
FROM Table_1 LEFT OUTER JOIN
Table_2 ON Table_1.[Test No] = Table_2.[Test No]
WHERE (Table_1.[Group No] = '123') AND (Table_2.[Sample No] = 'ABC')


Returns

[Group No] [Test No] [Description] [Result Description]
123 1 [First Test] [Some More Result]
123 3 [Third Test] [Some Result]

But what I really want is this:

[Group No] [Test No] [Description] [Result Description]
123 1 [First Test] [Some More Result]
123 2 [Second Test] NULL
123 3 [Third Test] [Some Result]

Am I doing this correctly??? Is this structured correct?:confused:
 
Back
Top Bottom