I want know how I can add an extra column to a Join based on a condition.
I am trying to do something like outline below. If a location is a county then I want all properties in the county; however, if the location is a town I want the properties in that town. Unfortunately town codes can be duplicated across counties, so I need to filter by county and town code.
SELECT DISTINCT [PropertyID]
FROM PropertyLocations
LEFT JOIN [dbo].[Locations]
ON [PropertyLocations].[CountyCode] = [Locations].[CountyCode]
-- IF / CASE locations.[LocationLevel] is 6 then
-- i want to join on a second column as well
-- [PropertyLocations].[CountySubCode] = [Locations].[SubCountyCode]
WHERE [LocationName] = 'county/town name'
AND [PropertyLocations].[CountyCode] = [Locations].[CountyCode]
order BY [PropertyID]
Is this what you need?