SQL条件JOIN列

Ada*_*itt 3 sql join case coalesce conditional-statements

我想根据当前行的值确定JOIN列.

例如,我的作业表有4个日期列:offer_date,accepted_date,start_date,reported_date.

我想根据日期检查汇率.我知道reported_date永远不会为null,但这是我的最后一招,所以我有一个优先顺序,可以加入对于exchange_rate表.我不太确定如何使用CASE声明,如果这是正确的方法.

INNER JOIN exchange_rate c1 ON c1.date = {{conditionally pick a column here}}

  -- use j.offer_date if not null
  -- use j.accepted_date if above is null
  -- use j.start_date if above two are null
  -- use j.reported_date if above three are null
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 9

尝试这样的逻辑:

INNER JOIN
exchange_rate c1
ON c1.date = coalesce(j.offer_date, j.accepted_date, j.start_date, j.reported_date)
Run Code Online (Sandbox Code Playgroud)

coalesce()函数返回列表中的第一个非NULL值.