查找仅在给定日期之前的记录(SQL)

时间:2019-02-07 15:42:14

标签: sql left-join exists outer-join

我已经尝试了多个查询,但是它们都不起作用。 这可能真的很简单。

这是一个示例表:

ordernr debnaam     debnr   orddat
1       Coca-Cola   123     2019-02-07
12      Altec       456     2019-02-07
123     Coca-Cola   123     2016-01-01
1234    Brady       789     2015-03-18

因此,关键是要查找自最近两年以来未订购的客户(debnaam)。 在我的示例中,唯一的记录应该是布雷迪。

我尝试了以下查询:

SELECT a.ordernr, a.debnaam, a.debnr, a.orddat 
FROM orkrg as a 
WHERE NOT EXISTS(SELECT b.debnr 
                 FROM orkrg as b 
                 WHERE a.ordernr = b.ordernr 
                 AND b.orddat > CONVERT(date, dateadd(year,-2,getdate())))

或具有左外部联接:

SELECT * 
FROM (
      SELECT orkrg.ordernr, orkrg.debnaam, orkrg.debnr, orkrg.orddat 
      FROM orkrg 
      WHERE orkrg.orddat < CONVERT(date, dateadd(year,-2,getdate()))
     ) AS a
LEFT OUTER JOIN 
     (
      SELECT orkrg.ordernr, orkrg.debnaam, orkrg.debnr, orkrg.orddat 
      FROM orkrg 
      WHERE orkrg.orddat > CONVERT(date, dateadd(year,-2,getdate()))
     ) as b 
  ON a.ordernr = b.ordernr

但是我总是得到以下结果:

ordernr debnaam    debnr    orddat
123     Coca-Cola  123      2016-01-01
1234    Brady      789      2015-03-18

有人可以帮我吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

您需要使用DATEDIFF()过滤掉较早的日期:

SELECT a.ordernr, a.debnaam, a.debnr, a.orddat 
FROM orkrg as a 
WHERE DATEDIFF(year, a.orddat, GETDATE()) > 2
AND A.debnr NOT IN (SELECT b.debnr FROM orkrg as b WHERE
DATEDIFF(year, b.orddat, GETDATE()) <= 2)

答案 1 :(得分:1)

select a.*
from orkrg as a
where a.orddat < dateadd(year,-2,getdate()) -- this is kinda not needed
and not exists (select 1  -- NOT EXISTS is a safer option than NOT IN, where a null result can cause issues
                from orkrg as b 
                where a.debnaam = b.debnaam and 
                b.orddat > dateadd(year,-2,getdate()))

默认情况下,我在not exists上使用not in,有关原因请参见here

答案 2 :(得分:1)

select * 
from orders as o 
where o.debnr not in (select debnr 
                      from orders as u 
                      where orddat > CONVERT(date, dateadd(year,-2,getdate())))