I am trying to map the World Bank country income levels to a dataset. The income levels change year to year and I need to map the appropriate income level to a transaction using the transaction's country and date.
The following tables illustrate the layout and problem:
Example - Table 1 Table 2
----------------- ----------------------------------------
|ISOCountryCode3, PODate | |CountryAlpha3, StartDate, EndDate, IncomeClass|
|CIV, '2009-11-01' | | CIV, 1989-10-02, 1989-09-12, lower |
|ALB, '2007-01-04' | | CIV, 2009-01-01, 2010-01-01, lower |
| CIV, 2010-01-02, 2011-01-01, middle |
The system should return:
|CIV, '2009-11-01', lower|
|...
Here's the SQL that I've tried so far
SELECT mergestandard.*,incomeclassifications.IncomeClass
FROM `mergestandard`
LEFT OUTER JOIN incomeclassifications
ON mergestandard.ISOCountryCode3 = incomeclassifications.CountryAlpha3
AND mergestandard.PODate<=incomeclassifications.EndDate
AND mergestandard.PODate>=incomeclassifications.StartDate
Unfortunately, the system keeps returning null in the incomeclass field. Any help would be much appreciated!
I don't usually use mySql, but I would try this:
SELECT mergestandard.*,incomeclassifications.IncomeClass
FROM `mergestandard`
LEFT OUTER JOIN incomeclassifications
ON mergestandard.ISOCountryCode3 = incomeclassifications.CountryAlpha3
WHERE
mergestandard.PODate<=incomeclassifications.EndDate
AND mergestandard.PODate>=incomeclassifications.StartDate
The first part would give you
|CIV, '2009-11-01', lower|
|CIV, '2009-11-01', lower|
|CIV, '2009-11-01', middle|
|...
Which actually also includes the dates
|CIV, '2009-11-01', lower| 1989-10-02, 1989-09-12
|CIV, '2009-11-01', lower| 2009-01-01, 2010-01-01
|CIV, '2009-11-01', middle| 2010-01-02, 2011-01-01
|...
Then use the where clause to filter what you want.
|CIV, '2009-11-01', lower| 2009-01-01, 2010-01-01
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.