3

我正在尝试将世界银行国家收入水平映射到数据集。收入水平每年都在变化,我需要使用交易的国家和日期将适当的收入水平映射到交易。

下表说明了布局和问题:

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

不幸的是,系统在收入类别字段中不断返回 null。任何帮助将非常感激!

4

1 回答 1

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
于 2013-09-19T20:40:50.627 回答