I am trying to list similar jobs based on the current job the user is viewing.
Current Job Location:
Local Zone: Vancouver Downtown
City: Vancouver
State: British Columbia
Now, I want to show similar jobs based on the following criteria :
Step 1: Filter jobs based on "Vancouver Downtown", "Vancouver" and "British Columbia" and return the 10 jobs order by ""Vancouver Downtown" localzone
Step 2: If we don't find 10 jobs in step 1 then we find jobs in "Vancouver" and "British Columbia" and return the 10 jobs order by "Vancouver".
Step 3: If we don't find 10 jobs in step 2 then we find jobs in "British Columbia" and return the top 10 jobs order by just "British Columbia".
So search order to find similar jobs starts from lowest level that is "Local Zone", then if not there, we move to "City" and vice versa.
In the final result, I want to list down the jobs starting from the lowest level "Local Zone" and then we move to "City" and then "State".
Table :
Jobs:
JobId Title LocationId Salary
Location:
LocationId CountryId StateId CityId LocalZoneId
Query :
DECLARE
@currentJobId INT = 101,
@currentJobLocalZoneId INT = 201,
@currentJobCityId INT = 301,
@currentStateId INT = 401
SELECT TOP 10
j.JobId,
j.Title,
j.Salary
l.StateId,
l.CityId,
l.LocalZoneId
FROM Jobs j
LEFT JOIN Location l ON l.LocationId = j.LocationId
WHERE
j.JobId != @currentJobId
AND (@currentJobLocalZoneId IS NULL OR l.LocalZoneId = @currentJobLocalZoneId)
AND (@currentJobCityId IS NULL OR l.CityId = @currentJobCityId)
AND (@currentStateId IS NULL OR l.StateId = @currentStateId)
ORDER BY
-- not sure how do i order results that starts from lowest level "Local Zone" and then we move to "City" and then "State"
But here in above query, I am confused with how do I implement bottom up approach of first starting from "Localzone" and if we don't find 10 jobs then we move to "City" and then "State".
Can someone please help or guide me to implement this part?