4
I have a fictitious table TB_META with the fields:
COD_VENDEDOR | DAT_ATIVACAO | VAL_META
Supposing 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 just one record of each ID. This record will be picked up based on the most current id date, 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 don’t think I can enjoy 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)
Paul, welcome to Stack Overflow. Could you explain better how your answer solves the problem by pointing out what has been fixed? You can [Dit] your question. I also recommend reading the [tour] and [help].
– gmsantos
"above" here is relative, Paul, the answers may be listed in 3 ways (see tabs at the top just below the question). Really, you need to explain better than you’re talking ;)
– brasofilo