选择尚未评估的代理商(按周计算)

时间:2015-10-11 19:14:00

标签: sql sql-server

尝试提取过去一周未评估(评分)的代理列表。我正在获取Agent_Name,但在检查评估列表时,它们的名称出现在评估列表中。

select agent.firstname + ' ' + agent.lastname Agent_Name
from dbo.agent agent
left outer join dbo.crr crr
on agent.id = crr.agentfk
left outer join dbo.evaluation eval
on crr.id = eval.crrfk
where eval.crrfk is null 
and crr.localtime >= Dateadd(Day, Datediff(Day, 0, Dateadd(D, -7,     Current_Timestamp)), 0);

1 个答案:

答案 0 :(得分:0)

您需要将crr上的条件移至on子句:

select agent.firstname + ' ' + agent.lastname Agent_Name
from dbo.agent agent left outer join
     dbo.crr crr
     on agent.id = crr.agentfk and
        crr.localtime >= Dateadd(Day, Datediff(Day, 0, Dateadd(D, -7,     Current_Timestamp)), 0) left outer join
     dbo.evaluation eval
     on crr.id = eval.crrfk
where eval.crrfk is null ;

如果数据位于evaluation,我不确定为什么需要crr表。我的意思是,这应该做同样的事情(假设你正在使用正确的日期):

select (a.firstname + ' ' + a.lastname) as Agent_Name
from dbo.agent a left outer join
     dbo.crr
     on a.id = crr.agentfk and
        crr.localtime >= Dateadd(Day, Datediff(Day, 0, Dateadd(D, -7,     Current_Timestamp)), 0) 
where crr.id is null ;