G
gerry
I have 2 SQL tables :
Data :
DataID ( key ) Lookup1 Lookup2
-------------- ------- -------
1 1 2
2 1 3
3 3 4
Lookup:
LookupId ( key ) LookupKey DataID
---------------- --------- ------
1 1 1
2 1 2
Here are 2 select statements and their respective output :
select
[Data].[DataID] ,
[Data].[Lookup1] ,
[Data].[Lookup2]
from [Data]
left join [Lookup]
on [Data].[Lookup1] = [Lookup].[DataID]
or [Data].[Lookup2] = [Lookup].[DataID]
where [Lookup].[LookupKey] = 1
order by [Data].[DataID]
DataID LookUp1 Lookup2
------ ------- -------
1 1 2
1 1 2
2 1 3
select
[Data].[DataID] ,
[Data].[Lookup1] ,
[Data].[Lookup2]
from [Data]
where
[Data].[Lookup1] in (select [DataID] from [Lookup] where [LookupKey] = 1 )
or
[Data].[Lookup2] in (select [DataID] from [Lookup] where [LookupKey] = 1 )
order by [Data].[DataID]
DataID LookUp1 Lookup2
------ ------- -------
1 1 2
2 1 3
The important difference between the 2 results being the uniqueness of the returned [Data] rows regardless of whether they matched [Lookup] on one or both of [Lookup1] / [Lookup2].
Maybe I am just having a brain fart, but it seems that I should be able to get the second results by tweaking the 1st select without requiring the 2 subqueries in the 2nd select.
Any suggestions would be appreciated.
Gerry
Data :
DataID ( key ) Lookup1 Lookup2
-------------- ------- -------
1 1 2
2 1 3
3 3 4
Lookup:
LookupId ( key ) LookupKey DataID
---------------- --------- ------
1 1 1
2 1 2
Here are 2 select statements and their respective output :
select
[Data].[DataID] ,
[Data].[Lookup1] ,
[Data].[Lookup2]
from [Data]
left join [Lookup]
on [Data].[Lookup1] = [Lookup].[DataID]
or [Data].[Lookup2] = [Lookup].[DataID]
where [Lookup].[LookupKey] = 1
order by [Data].[DataID]
DataID LookUp1 Lookup2
------ ------- -------
1 1 2
1 1 2
2 1 3
select
[Data].[DataID] ,
[Data].[Lookup1] ,
[Data].[Lookup2]
from [Data]
where
[Data].[Lookup1] in (select [DataID] from [Lookup] where [LookupKey] = 1 )
or
[Data].[Lookup2] in (select [DataID] from [Lookup] where [LookupKey] = 1 )
order by [Data].[DataID]
DataID LookUp1 Lookup2
------ ------- -------
1 1 2
2 1 3
The important difference between the 2 results being the uniqueness of the returned [Data] rows regardless of whether they matched [Lookup] on one or both of [Lookup1] / [Lookup2].
Maybe I am just having a brain fart, but it seems that I should be able to get the second results by tweaking the 1st select without requiring the 2 subqueries in the 2nd select.
Any suggestions would be appreciated.
Gerry