我正在尝试将世界银行国家收入水平映射到数据集。收入水平每年都在变化,我需要使用交易的国家和日期将适当的收入水平映射到交易。
下表说明了布局和问题:
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 |
系统应返回:
|CIV, '2009-11-01', lower|
|...
这是我到目前为止尝试过的SQL
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
不幸的是,系统在incomeclass字段中保持返回null。任何帮助将不胜感激!
答案 0 :(得分:0)
我通常不使用mySql,但我会尝试这个:
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
第一部分会给你
|CIV, '2009-11-01', lower|
|CIV, '2009-11-01', lower|
|CIV, '2009-11-01', middle|
|...
实际上还包括日期
|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
|...
然后使用where子句过滤您想要的内容。
|CIV, '2009-11-01', lower| 2009-01-01, 2010-01-01