假设我有一个表应用程序如下
ApplicationId INT,
CustomerId INT,
ApplicationDate DATETIME,
IsNewCustomer BOOL
我想检索给定日期的应用程序列表以及IsNewCustomer标志(如果给定的 CustomerId 没有应用程序,则设置该标志。
目前我在同一张表上使用如下连接
select
o.ApplicationId as ApplicationId,
cast(o.ApplicationDate as date) as ApplicationDate,
case when n.ApplicationID is not null then 1 else 0 end as IsNewCustomer,
row_number() over (partition by o.ApplicationId order by o.ApplicationDate desc) as AppSeqNum
from
Applications o
left join Applications n
on n.CustomerId = o.CustomerId
and n.ApplicationDate < o.ApplicationDate
where
AppSeqNum = 1
and ApplicationDate = getdate()
我想知道是否有更好的方法来实现相同的目标,而不必加入同一张桌子,因为它“感觉”不是最优雅的解决方案。
谢谢!