如何获得最近X天未联系的联系人?

时间:2018-10-09 12:18:05

标签: sql sql-server

尽管我已经使用not exists和其他方法,但我仍在努力寻找一种可靠的方法来获取最近XXX天未联系的联系人的列表,

这是我的查询和结果快照

SELECT   top 100   

Todo_tbl.todo_ID,
Contact_tbl.Contact_ID,  ---e.i 34
Contact_tbl.Contact_Name, ---e.i John papa
Todo_tbl.Title, ---e.i Sent him an email
Todo_tbl.StartDate ---e.i 2018-10-10 16:50:18.017

FROM            

Todo_tbl INNER JOIN
Contact_tbl ON Todo_tbl.Contact_ID = Contact_tbl.Contact_ID


WHERE Todo_tbl.StartDate>60 <-- **wrong**
order by Todo_tbl.StartDate desc

todo_ID Contact_ID  Contact_Name                Title       StartDate
646555  14755       Fox Browne                  Phone       2018-06-06 16:50:18.017
646553  31791       Weickl                      Email       2018-06-06 16:47:03.000
646551  26735       Nexp                        Email       2018-06-06 16:42:19.000
646550  33264       Stooo                       Email       2018-06-06 16:40:54.000
646548  14769       Heyuu                       Email       2018-06-06 16:39:19.000
646547  14738       Dakota                      Email       2018-06-06 16:37:49.000
646560  36720       IHelo                       Email       2018-06-06 16:00:00.000

2 个答案:

答案 0 :(得分:1)

尝试左联接并使用条件Todo_tbl.Contact_ID为空

SELECT   top 100   

Todo_tbl.todo_ID,
Contact_tbl.Contact_ID,  
Contact_tbl.Contact_Name, -
Todo_tbl.Title, 
Todo_tbl.StartDate 

FROM   Contact_tbl left join         
Todo_tbl on
Todo_tbl.Contact_ID = Contact_tbl.Contact_ID
WHERE Todo_tbl.StartDate< cast(GETDATE()-7 as date) and Todo_tbl.Contact_ID is null
order by Todo_tbl.StartDate

答案 1 :(得分:0)

我相信您正在寻找:

MySQL

   ...
        WHERE Todo_tbl.StartDate <now() - interval 7 day

SQL Server

..
WHERE Todo_tbl.StartDate <GETDATE()-7