如何使用其他表中的值获取不匹配的记录(以及匹配的记录)?日期条件在BETWEEN运算符中指定

时间:2019-04-02 13:24:16

标签: sql postgresql join

我有两个表user_updated_detailsuser_details。我必须从updated_file_cap表中获取特定date_current的{​​{1}}和user_updated_details列值,以及使用user_id运算符指定的日期。

对于具有BETWEEN列的匹配记录,我正在获取记录。对于不匹配的日期,我应该从user_details表中获取值。不确定如何执行此操作。请协助

考虑date_current表有2条记录,例如

user_details

考虑user_id | user_name | file_cap ------------------------------- 111 | Karthik | 5 --------------------------------- 222 | Vinod | 6 -------------------------------- 表有3条记录,例如

user_updated_details

要查找匹配的记录,我们可以使用以下查询。

   user_id  |  date_current   |  updated_file_cap
  ---------------------------------------------
    111     |   2019-03-29   |  10
    222     |   2019-03-29   |  11
    111     |  2019-04-01    |  9
   ----------------------------------------------

当前结果集将是:

select date_current  , updated_file_cap, user_id 
from user_updated_details 
where user_id=111 
and date_current  BETWEEN '2019-04-02' AND '2019-03-29' 
ORDER BY date_current  DESC;

但是我的要求是,对于不匹配的值(在 date_current | updated_file_cap | user_id ------------------------------------------ 2019-03-29 | 10 | 111 2019-04-01 | 9 | 111 ------------------------------------------ 运算符中指定),例如 2019-03-30 2019-03 -31 2019-04-02 我应该从user_details表的file_cap列中获取updated_file_cap值。

我需要一个查询,该查询给出预期输出应如下所述

BETWEEN

0 个答案:

没有答案