【问题标题】:also include records in this table that match condition on another table in sql还包括此表中与 sql 中另一个表上的条件匹配的记录
【发布时间】:2018-10-04 08:45:00
【问题描述】:

我在这些行中有表格:

答:

id   param_1   param_2    status     dateUpdated
-----------------------------------------------
1    valuea   valueb    Active      2018-10-02
2    valuec   valued    Inactive    2018-09-03
3    valuee   valuef    Active      2018-10-01
4    valueg   valueh    Active      2017-01-20
5    value1   value2    Active      2018-03-03
6    value3   value4    Active      2016-10-21
...

乙:

id    a_id   some_param    dateModified
-------------------------------------
1     3      x             2018-10-04 
2     2      y             2018-06-30
3     4      aa            2018-10-01
...
99    6      ab            2018-01-16
100   3      z             2018-04-08

我想要A 中的记录在某个日期之后更新,但还包括与另一个表B 中的日期条件匹配的其他记录(如果不存在,则返回最新日期)。

如果只有表A:

select id,
       param_1, 
       param_2, 
       dateUpdated
from A
where status = 'Active'
and dateUpdated between @someDate and @someAnotherDate

现在加入:

select distinct  A.id,
                 A.param_1, 
                 A.param_2, 
                 A.dateUpdated -- or B.dateModified, whichever is latest if that's even possible
from A
join B on B.a_id = A.id
where A.status = 'Active'
and ((A.dateUpdated between @someDate and @someAnotherDate) || (B.dateModified between @someDate and @someAnotherDate))

所以从上面说,someDate = '2018-10-01'someAnotherDate = '2018-10-04' 我会得到结果:

id,  param_1   param_2    dateUpdated
------------------------------------
1    valuea   valueb      2018-10-02
3    valuee   valuef      2018-10-04
4    valueg   valueh      2018-10-01

【问题讨论】:

  • 如果日期不在范围内,您是否会包含 ID 为 3 的 A 记录? IE。在 B 中有这样一个日期的 a_id 3 记录就足够了吗?
  • @ThorstenKettner 我已将问题更新为包括 4、5 和 6。我想这应该可以满足几乎所有可能的情况。如果它回答了您的问题,请告诉我。
  • 好的。我已经更新了我的答案。使用我展示的更改后的 WHERE 子句,您还将获得 ID 4。
  • 好的。谢谢!。一有机会坐下,我会尽快尝试答案。
  • 您能再解释一下标准吗?据我所知,您希望第 1 行和第 3 行直接满足条件,但加入后将有两行 3。您要显示哪一行?

标签: sql sql-server sql-server-2016


【解决方案1】:

B 开始,您需要a_id 给定范围内的最大日期。将此结果外连接到A 并使用CASE WHEN 获取更新的日期。

select
  a.id,
  a.param_1,
  a.param_2,
  a.status,
  case when bmax.max_date > a.dateUpdated then bmax.max_date else a.dateUpdated as updated
from a
left join
(
  select a_id, max(dateModified) as max_date
  from b
  where dateModified between @someDate and @someAnotherDate
  group by a_id
) bmax on bmax.a_id = a.id
where a.status = 'Active'
and 
(
   a.dateUpdated between @someDate and @someAnotherDate
   or
   bmax.dateModified between @someDate and @someAnotherDate
)
order by a.id;

【讨论】:

  • 一个简单的问题。是否已经为select a_id, max(dateModified) as max_date .. 选择了正确的范围。我们是否应该在where 子句中再次包含过滤?即bmax.dateModified between @someDate and @someAnotherDate
  • 是的,我们必须这样做,因为 bmax 是外连接的。
【解决方案2】:

使用左连接和大小写表达式获取最新日期

select distinct  A.id,
                 A.param_1, 
                 A.param_2, 
                 case when A.dateUpdated > B.dateUpdated
                 then A.dateUpdated else B.dateUpdated end

from A
left join B on B.a_id = A.id
where A.status = 'Active'
and ((A.dateUpdated between @someDate and @someAnotherDate) 
    OR (B.dateModified between @someDate and @someAnotherDate))

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多