I need to do a JOIN with a ‘near match’. The best way to explain this is with an example:
CREATE TABLE Car
(
Vin int,
Make nvarchar(50),
ColorID int,
)
CREATE TABLE Color
(
ColorID int,
ColorCode nvarchar(10)
)
CREATE TABLE ColorName
(
ColorID int,
Languagecode varchar(12),
ColorName nvarchar(50)
)
INSERT INTO Color Values (1, 'RED CODE')
INSERT INTO Color Values (2, 'GREEN CODE')
INSERT INTO Color Values (3, 'BLUE CODE')
INSERT INTO ColorName Values (1, 'en', 'Red')
INSERT INTO ColorName Values (1, 'en-US', 'Red, my friend')
INSERT INTO ColorName Values (1, 'en-GB', 'Red, my dear')
INSERT INTO ColorName Values (1, 'en-AU', 'Red, mate')
INSERT INTO ColorName Values (1, 'fr', 'Rouge')
INSERT INTO ColorName Values (1, 'fr-BE', 'Rouge, mon ami')
INSERT INTO ColorName Values (1, 'fr-CA', 'Rouge, mon chum')
INSERT INTO Car Values (123, 'Honda', 1)
The SPROC would look like this:
DECLARE @LanguageCode varchar(12) = 'en-US'
SELECT * FROM Car A
JOIN Color B ON (A.ColorID = B.ColorID)
LEFT JOIN ColorName C ON (B.ColorID = C.ColorID AND C.LanguageCode = @LanguageCode)
See http://sqlfiddle.com/#!6/ac24d/24 (thanks to Jake!)
Here is the challenge:
When the SPROC parameter @LanguageCode is an exact match, all is well.
I would like for it to also work for partial matches; more specifically: say for example that @LanguageCode would be ‘en-NZ’ then I would like the SPROC to return the value for language code ‘en’ (since there is no value for ‘en-NZ’).
As an extra challenge: if there is no match at all I would like to return the ‘en’ value; for example if @LanguageCode would be ‘es’ then the SPROC would return the ‘en’ value (since there is no value for ‘es’).
As @Roman Pekar has said in his comment, this can indeed be done, including your additional request about falling back to
en, in one statement with the help of a ranking function. Here’s how you could go about it:;
That is, the
ColorNametable is filtered and ranked before being used in the query, and then only the rows with the rankings of1are joined:The filter for
ColorNameincludes only rows withLanguageCodevalues of@LanguageCode,LEFT(@LanguageCode, 2)and'en'.The ranking values are assigned based on which language code each row contains: rows with
LEFT(@LanguageCode, 2)are ranked after those with@LanguageCodebut before the'en'ones.