***TABLE LEAD***
ID || LID || LNAME || STATUS || TIME
------------------------------------------------------
1 || 22 || LA12 || SUCCESS || 11:40
2 || 56 || LA13 || ERROR 1 || 14:23
3 || 89 || LA45 || ERROR 2 || 18:30
4 || 100 || LA56 || SUCCESS || 10:20
***Table ACTIVITY***
ID || ACTIDID || LNAME || STATUS || TIME
-----------------------------------------------------
1 || 24 || LA12 || ERROR 1 || 11:10
2 || 25 || LA12 || ERROR 2 || 11:11
3 || 26 || LA12 || ERROR 1 || 11:40
4 || 58 || LA13 || ERROR 1 || 14:23
5 || 91 || LA45 || ERROR 2 || 18:30
6 || 102 || LA56 || ERROR 2 || 10:15
7 || 103 || LA56 || ERROR 3 || 10:20
我有两个表(LEAD& Activity),活动表有多个针对潜在客户的活动。
所以,我希望那些来自LEAD TABLE WHERE LEAD STATUS的行!=最后的活动状态
LNAME
LA12
LA56
答案 0 :(得分:0)
使用子查询确定活动表中每个lname的最大ID,在活动表上加入此状态以获取状态,并在lname上的引导表上将其加入以获取潜在客户状态。在where标准过滤那些,其中铅和活动状态不同。
select l.lname
from lead l
left join (select max(id) as maxid, lname from activity group by lname) m on l.lname=m.lname
left join activity a on m.lname=a.lname and m.maxid=a.id
where a.status<>l.status or a.status is null