I have set up the following DB2 table, which is intended to store codes and their corresponding descriptions, and to allow for multiple languages and/or countries.
DECLARE CODES TABLE (
CATEGORY INTEGER NOT NULL,
CODE CHAR(08) NOT NULL,
LANGUAGE CHAR(03) NOT NULL,
COUNTRY CHAR(03) NOT NULL,
DESCRIPTION VARCHAR(64) NOT NULL)
I added a unique primary key to this table as follows:
CATEGORY ASC,
CODE ASC,
LANGUAGE DESC,
COUNTRY DESC
OK, Fine so far. Now I complicate things by specifying that a description might be specific to a language and a country, a language only or it may be a defaulted American English description.
My problem is this. I want to obtain a complete list of codes for a given category, along with their descriptions. The following SQL:
SELECT CODE, DESCRIPTION
FROM CODES
WHERE CATEGORY = :CATEGORY
AND LANGUAGE IN (:LANGUAGE, ' ')
AND COUNTRY IN (:COUNTRY, ' ')
Could, in theory, return up to four rows per code. The requirement is to return one row per code, using the maximum language/country sub-key to select the appropriate description. The most efficient way to do this would be to get DB2 to somehow perform a skip-sequential read of the primary index, taking the first index per code. However, all my solutions so far have involved multiple levels of subselects and liberal use of the MAX() function, and don't strike me as efficient by any stretch of the imagination.
So can anyone point me towards a better solution.