I am trying to perform a query where in one of the columns ( COTA_DIA_ANTERIOR
) returns me the value of the previous day. But it's bringing the value of the day.
SELECT
ET.DT_REFERENCIA
,ET.VL_PU
,(SELECT TOP (1) [DT_REFERENCIA] FROM [TRUST].[dbo].[MTM_RFIXA] WHERE [NR_ATIVO] = ET.NR_ATIVO AND DT_REFERENCIA < ET.DT_REFERENCIA ORDER BY DT_REFERENCIA DESC) as DIA_ANTERIOR
,(SELECT TOP(1) ET.VL_PU FROM MTM_RFIXA WHERE DT_REFERENCIA = (SELECT TOP (1) [DT_REFERENCIA] FROM [TRUST].[dbo].[MTM_RFIXA] WHERE [NR_ATIVO] = ET.NR_ATIVO AND DT_REFERENCIA < ET.DT_REFERENCIA ORDER BY DT_REFERENCIA DESC) as COTA_DIA_ANTERIOR
FROM MTM_RFIXA ET
WHERE
NR_ATIVO = '2002'
GROUP BY DT_REFERENCIA, VL_PU
ORDER BY DT_REFERENCIA DESC