我有 2 个表 ( AllClients
& AllActivities
),需要检索以下信息:
我需要一个不同客户的列表,其中最近的活动是在去年输入的。我已经让下面的代码工作了,但它非常慢,因此没有用。我相信加入(没有子查询)会更快,但我就是想不通。这是我当前的sql语句:
select distinct(AllClients.LookupCode)
from AllClients
join (select LookupCode,
max(AllActivities.EnteredDate) as EnteredDate
from AllActivities
group by LookupCode) AllActivities
on AllClients.LookupCode = AllActivities.LookupCode
where AllClients.Name = '$userName'
and AllClients.TypeCode = 'P' and AllActivities.EnteredDate < '$oneYearAgo'";