我有一个表,该表在不同点跟踪资产,但是我需要找出资产何时到达第4点,我需要知道何时最后一次在第1点看到资产。
我尝试获取资产位于第1点且日期小于第4点的日期的最大日期,但无济于事。
with CTE as
( select distinct TAG, UID_KEG, CONVERT(VARCHAR(10),Movement_date,105) as
Inbound, L.CAPTION
from MOVEMENT M1
inner join KEG K on M1.UID_KEG = K.UNIQUE_ID
inner join STATION S on M1.UID_STATION = S.UNIQUE_ID
Inner join LOCATION L on S.UID_LOCATION = L.UNIQUE_ID
where DATEPART(year,movement_date) = '2019'
and DATEPART(Month,MOVEMENT_DATE) = '07'
and UID_MOVEMENT_TYPE = 4
)
select CTE.TAG, CTE.Inbound,
MAX(Convert(VARCHAR(10),M2.MOVEMENT_DATE,105)) as 'Last Seen'
from MOVEMENT M2
Inner join CTE on M2.UID_KEG = CTE.UID_KEG
inner join KEG K2 on M2.UID_KEG = K2.UNIQUE_ID
inner join STATION S2 on M2.UID_STATION = S2.UNIQUE_ID
Inner join LOCATION L2 on S2.UID_LOCATION = L2.UNIQUE_ID
where UID_MOVEMENT_TYPE = 1
and Convert(VARCHAR(10),M2.MOVEMENT_DATE,105) < Inbound
group by CTE.TAG, CTE.Inbound
理想情况下,我想要的是资产标签列表,第4点的日期,然后第1点的日期,然后可以链接到其他表并收集更多信息。
TAGID, Point 4, Point 1
1234, 06/07/2019, 10/03/2019
45678, 5/04/2019, 01/12/2018