Hi @fabien prieur ,
SELECT *,
CASE WHEN (DATEDIFF(DAY,CHANGE_DATE,newdate)) <0 THEN DATEDIFF(DAY,CHANGE_DATE,newdate)*-1 ELSE DATEDIFF(DAY,CHANGE_DATE,newdate) END AS timeelapsed
FROM (SELECT
NF.SYSTEM_NAME AS PRODUCT,
OTH.OBJECT_IDENT NB,
OTH.CURRENT_STATUS,
OTH.DOCINDEXDATE CHANGE_DATE,
lag(CHANGE_DATE,1,getdate()) OVER (ORDER BY NB) as newdate
FROM CSA_ECPBASEVIEW_NF NF
INNER JOIN OT_STATUSHISTORY OTH
ON OTH.OBJECT_IDENT = NF.SONREFERENCE) a
The code should be a entirety part and you could not execute them separately.
In other words, the code part is used as table a shown below, in your second part the from table is lost which caused the error.
(SELECT
NF.SYSTEM_NAME AS PRODUCT,
OTH.OBJECT_IDENT NB,
OTH.CURRENT_STATUS,
OTH.DOCINDEXDATE CHANGE_DATE,
lag(CHANGE_DATE,1,getdate()) OVER (ORDER BY NB) as newdate
FROM CSA_ECPBASEVIEW_NF NF
INNER JOIN OT_STATUSHISTORY OTH
ON OTH.OBJECT_IDENT = NF.SONREFERENCE) a
Regards,
Zoe