i'm trying to get only the most recent location of an asset, I found this, following that example. I did this:
ALTER PROCEDURE [SISACT].[index_activo_ubicacion]
@ubicado BIT = 1
AS
BEGIN
IF @ubicado = 1
SELECT activo,
ubicacion
FROM
(SELECT activo,
ubicacion,
Fecha_Ubicacion,
row_number() OVER (partition BY activo
ORDER BY abs(datediff(dd, Fecha_Ubicacion, getdate())))
FROM [SISACT].ACTIVO_UBICACION)
ELSE
SELECT Activo FROM [SISACT].ACTIVOS_FIJOS a
WHERE NOT EXISTS (
SELECT Activo FROM [SISACT].ACTIVO_UBICACION u
WHERE a.Activo = u.Activo
)
END
But I'm getting an error sintax near "ELSE", meaning the error is here:
SELECT activo,
ubicacion
FROM
(SELECT activo,
ubicacion,
Fecha_Ubicacion,
row_number() OVER (partition BY activo
ORDER BY abs(datediff(dd, Fecha_Ubicacion, getdate())))
FROM [SISACT].ACTIVO_UBICACION)
but i have no idea what i'm doing wrong since I'm fairly new to TSQL. Can anyone help me?
In the table ACTIVO_UBICACION there will be many records with the same ACTIVO but different Fecha_Ubicacion, I want to select all ACTIVO but only the ones with the most recent date. Meaning
Activo | Ubicacion | Fecha_Ubicacion
------------------------------------
Activ | 1244 | 2012-04-02 15:42:33
FixedA | 4556 | 2012-05-05 13:33:34
Activ | 4553 | 2012-05-06 22:11:45
FixedA | 2S01 | 2012-07-10 12:44:20
Activ | 1114 | 2013-03-14 14:33:45
Meaning it should return
Activo | Ubicacion | Fecha_Ubicacion
------------------------------------
FixedA | 2S01 | 2012-07-10 12:44:20
Activ | 1114 | 2013-03-14 14:33:45
Thank you in advance