The following query returns the following:
IFobject_id('tempdb..#tmpHistorico')ISNOTNULLBEGINDROPTABLE#tmpHistoricoENDCREATETABLE#tmpHistorico(Idintnotnull,IdColuna1intnotnull,IdColuna2intnotnull,Valor1varchar(10)notnull,Valor2varchar(10)notnull,DataAtualizacaoDateTimenotnull)insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,1,1,'100','2000','2018-04-0900:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,2,2,'100','99999','2018-04-0900:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,2,2,'100','200000','2018-04-1000:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,3,3,'100','259999','2018-04-0900:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,4,4,'100','109999','2018-04-0900:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,4,4,'100','9999','2018-04-1000:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(1,5,5,'100','2559999','2018-04-0900:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(2,6,6,'100','159999','2018-04-0900:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(2,6,6,'100','309999','2018-04-1000:00:00.000')insert#tmpHistorico(Id,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacao)values(2,7,7,'100','559999','2018-04-0900:00:00.000')selectId,IdColuna1,IdColuna2,Valor1,Valor2,DataAtualizacaofrom#tmpHistoricowhereId=1
DOUBTS
HowdoIdeleteduplicaterows?
IneedthequerytoreturnthelineswithDataAtualizacao
Latestanddeletetheoldest,andlooklikethis: