Ok, so this particular use case is giving me quite a bit of headache.
This is a edit of another post, where i forgot to add an important aspect to the usecase.
What is need to do is match a Candidates WorkWish with the CustomerJobQueries in the database.
This means match if the DateTimes in the WorkWish is overlapping with any of the CustomerJobQueries. then i need to match the GeographicalPreference with the location of the Customer.
The logic we had when we build the database was this:
- if the Country is set but the region and municipality is NULL - the candidate wishes to work in the entire country.
- if the country and region is set the candidate wishes to work in all municipalities in the specific region in the specific country.
- if the country, region and municipality is set he wishes to only work in that municipality.
It is possible for multiple rows to exists such that 2 specific municipalities is set, the only rule is the there will never exist a row that takes precedence over another.
meaning
GeographicalPreferenceId Country_ShortName Region_Id Municipality_Id WorkWish_Id
------------------------ -------------------- ----------- --------------- -----------
1 DK NULL NULL 1
2 DK 3 NULL 1
Is NOT possible because one row already specifies he wishes to work in the entire country.
This is how a snippet of the database looks like
DateRange Table -- contains all DateRanges
DateRangeId StartDate EndDate CustomerJobQuery_Id WorkWish_Id
----------- --------------------------- --------------------------- ------------------- -----------
1 2014-03-31 00:00:00.0000000 2014-08-18 00:00:00.0000000 NULL 1
2 2014-08-25 00:00:00.0000000 2014-09-15 00:00:00.0000000 NULL 1
3 2013-08-24 00:00:00.0000000 2014-09-25 00:00:00.0000000 1 NULL
Workwish Table
Id Candidate_Id
----------- ------------
1 5
CustomerJobQuery Table
Id Customer_CustomerId Country_ShortName Region_Id Municipality_Id
----------- ------------------- ----------------- --------- -----------------
1 2 DK 2 3
GeographicalPreference Table
GeographicalPreferenceId Country_ShortName Region_Id Municipality_Id WorkWish_Id
------------------------ -------------------- ----------- --------------- -----------
1 DK 2 NULL 1
2 DK 3 NULL 1
What is have so far is; - Returns the CustomerJobQueries that overlap with the WorkWish dates.
SELECT c.*
FROM CustomerJobQuery c
WHERE EXISTS (
SELECT *
FROM Workwish w
INNER JOIN DateRange d1 ON d1.CustomerJobQuery_Id = c.Id
INNER JOIN DateRange d2 ON d2.WorkWish_Id = w.Id
WHERE w.Candidate_Id = @CandidateId -- input from my **StoredProcedure**
-- overlap check:
AND d1.StartDate <= d2.EndDate
AND d2.StartDate <= d1.EndDate
)
I am uncertain how i would do the geographicalpreference logic in a setbased manner. - currently our solution is done in C# loading everything into memory and looping over everything with nested foreaches which is horribly ineffective. And is an absolute worst case scenario.
Any help is much appreciated!
DateRange
table joining on the two id columns? This looks ver suspicious. Very strange data structure.