SQL条件连接列

亚当·莱维特(Adam Levitt)

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

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

我想根据日期查询汇率。我知道reported_date永远不会为空,但这是我的最后选择,因此我有一个优先顺序要加入到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
戈登·利诺夫(Gordon Linoff)

尝试这样的逻辑:

INNER JOIN
exchange_rate c1
ON c1.date = coalesce(j.offer_date, j.accepted_date, j.start_date, j.reported_date)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章