I have a dummy TB_META table with the fields:
COD_VENDEDOR | DAT_ATIVACAO | VAL_META
Assuming I have these values in the table:
1 | 2011-01-01 | 1
1 | 2014-04-04 | 2
2 | 2012-01-01 | 3
2 | 2013-03-03 | 4
3 | 2013-01-01 | 5
3 | 2014-04-04 | 6
I want to get only one record of each ID. This record will be taken based on the most current date of the id, generating this result
1 | 2014-04-04 | 2
2 | 2013-03-03 | 4
3 | 2014-04-04 | 6
My attempt was like this, but I do not think I can take advantage of any of this:
SELECT DISTINCT TQ.[COD_VENDEDOR]
,TQ.[VAL_META]
,TQ.[DAT_ATIVACAO]
FROM
[dbo].[TB_META] TQ
INNER JOIN
[dbo].[TB_VENDEDOR] TV
ON TQ.COD_VENDEDOR = TV.COD_VENDEDOR
WHERE
TQ.DAT_ATIVACAO = (SELECT TOP 1 DAT_ATIVACAO FROM TB_META WHERE COD_VENDEDOR = TQ.COD_VENDEDOR ORDER BY DAT_ATIVACAO)