This is a little hard to explain so please bear with me.
I have 4 tables connected as follows:
Addresses
Streets
Segments
ESNs
Addresses.StreetId = Streets.Id
Streets.Id = Segments.StreetId
Segments.ESNId = ESNs.Id
I am running a query that I want to retrieve all addresses regardless of whether or not they fall in a segment. (A segment may have a beginning address of 100 and ending address of 200, and an address may fall within that range). So I used a left join. However, my problem now is this:
Say there are 4 segments for the same street with 4 different ESNs. When I run my query I get the same address 4 times. One for each ESN connected to a segment with the same streetId as the address.
I want each address once. The query should only pick up the ESN from the segment the corresponding address falls in. If I use WHERE Addresses.Address BETWEEN Segments.BAddress AND Segments.EAddress, then I only get the addresses that fall within segments. Not the ones that don't.
Here is my sql stmt:
SELECT DISTINCT [Streets].[City], [Streets].[Name], [Streets].[Type], [Streets].[Name]+' '+[Streets].[Type] AS Street, [Addresses].[Address],...ETC...[ESNs].[ESN]
FROM Addresses LEFT JOIN (Streets LEFT JOIN (Segments LEFT JOIN ESNs ON [Segments].[ESNId]=[ESNs].[Id]) ON [Streets].[Id]=[Segments].[StreetId]) ON [Addresses].[StreetId]=[Streets].[Id]
WHERE [Streets].[City]="RAEFORD" And [Addresses].[Address]
ORDER BY [Streets].[Name], [Streets].[Type], [Addresses].[Address]
Is there a way to write this statement so that it picks up all addresses once, but also retrieves the ESN if the address falls within a segment?
Any ideas would be appreciated. Thanks.
I have 4 tables connected as follows:
Addresses
Streets
Segments
ESNs
Addresses.StreetId = Streets.Id
Streets.Id = Segments.StreetId
Segments.ESNId = ESNs.Id
I am running a query that I want to retrieve all addresses regardless of whether or not they fall in a segment. (A segment may have a beginning address of 100 and ending address of 200, and an address may fall within that range). So I used a left join. However, my problem now is this:
Say there are 4 segments for the same street with 4 different ESNs. When I run my query I get the same address 4 times. One for each ESN connected to a segment with the same streetId as the address.
I want each address once. The query should only pick up the ESN from the segment the corresponding address falls in. If I use WHERE Addresses.Address BETWEEN Segments.BAddress AND Segments.EAddress, then I only get the addresses that fall within segments. Not the ones that don't.
Here is my sql stmt:
SELECT DISTINCT [Streets].[City], [Streets].[Name], [Streets].[Type], [Streets].[Name]+' '+[Streets].[Type] AS Street, [Addresses].[Address],...ETC...[ESNs].[ESN]
FROM Addresses LEFT JOIN (Streets LEFT JOIN (Segments LEFT JOIN ESNs ON [Segments].[ESNId]=[ESNs].[Id]) ON [Streets].[Id]=[Segments].[StreetId]) ON [Addresses].[StreetId]=[Streets].[Id]
WHERE [Streets].[City]="RAEFORD" And [Addresses].[Address]
ORDER BY [Streets].[Name], [Streets].[Type], [Addresses].[Address]
Is there a way to write this statement so that it picks up all addresses once, but also retrieves the ESN if the address falls within a segment?
Any ideas would be appreciated. Thanks.