可能最快的解决方案是使用ISNULL。 你写的地方 MAX(ControlDate) 延长它 MAX(ISNULL(ControlDate,'1970-01-01')) (或您拥有的任何默认日期)
MAX(ControlDate)
MAX(ISNULL(ControlDate,'1970-01-01'))
这将替换NULL并使您的查询工作。
我希望它有所帮助。 彼得
您可以使用 TOP 1 WITH TIES 在...上 ROW_NUMBER 排序以获取每个客户的最新日期记录。
TOP 1 WITH TIES
ROW_NUMBER
select c.customerid, c.name, a.countrycode, case when c.actorid in (select * from agreement where activeagreement = 'Y' and product in ('6774', '6775')) then 1 else 0 end as hasaccount, i.controldate, i.controlby from customer c left join agreement a on a.actorid = c.actorid left join ( select top 1 with ties * from identification order by row_number() over (partition by actorid order by controldate desc) ) i on i.actorid = c.actorid where c.customerstatus = 'Active';
的 更新: 强> 以上答案对OP没有用,所以我提供了以下两个有效的替代方案:
left join ( select actorid, controlby, controldate, max(controlby) over (partition by actorid) as max_controldate from identification ) i on i.actorid = c.actorid and i.controldate = i.max_controldate.
和
left join ( select * from identification qualify row_number() over (partition by actorid order by controldate desc) = 1) ) i on i.actorid = c.actorid. 锟紺 Thorsten
最后一个选项 QUALIFY 是teradata的方式来做到这一点。 QUALIFY 是SQL标准的teradata扩展。另外两种方法是标准SQL。
QUALIFY