How to delete duplicate lines without using distinct?

2

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?

IneedthequerytoreturnthelineswithDataAtualizacaoLatestanddeletetheoldest,andlooklikethis:

    
asked by anonymous 10.04.2018 / 09:49

2 answers

6

Try the following:

SELECT t1.*
  FROM #tmpHistorico t1
 WHERE t1.DataAtualizacao = (SELECT MAX(t2.DataAtualizacao)
                               FROM #tmpHistorico t2
                              WHERE t2.IdColuna1 = t1.IdColuna1
                                AND t2.IdColuna2 = t1.IdColuna2);

To make this solution more performative, you can index the columns DataAtualizacao , IdColuna1 and IdColuna2 .

    
10.04.2018 / 12:46
1

I suggest you create a new temporary table to control what will or will not be displayed, since this can not be done by Id .

IF object_id('tempdb..#tmpHistorico') IS NOT NULL BEGIN DROP TABLE #tmpHistorico END
IF object_id('tempdb..#tmpHistoricoMaxData') IS NOT NULL BEGIN DROP TABLE #tmpHistoricoMaxData END

CREATE TABLE #tmpHistorico
(
    Id              int not null,
    IdColuna1       int not null,
    IdColuna2       int not null,
    Valor1          varchar(10) not null,
    Valor2          varchar(10) not null,
    DataAtualizacao DateTime not null
)

insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,1,1,'100',    '2000','2018-04-09 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,2,2,'100',   '99999','2018-04-09 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,2,2,'100',  '200000','2018-04-10 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,3,3,'100',  '259999','2018-04-09 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,4,4,'100',  '109999','2018-04-09 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,4,4,'100',    '9999','2018-04-10 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (1,5,5,'100', '2559999','2018-04-09 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (2,6,6,'100',  '159999','2018-04-09 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (2,6,6,'100',  '309999','2018-04-10 00:00:00.000')
insert #tmpHistorico (Id, IdColuna1, IdColuna2, Valor1, Valor2, DataAtualizacao) values (2,7,7,'100',  '559999','2018-04-09 00:00:00.000')

CREATE TABLE #tmpHistoricoMaxData
(
    Id              int not null,
    IdColuna1       int not null,
    IdColuna2       int not null,
    DataAtualizacao DateTime not null
)

insert into #tmpHistoricoMaxData
select Id, IdColuna1, IdColuna2, max(DataAtualizacao) 
from #tmpHistorico
group by Id, IdColuna1, IdColuna2

select tmp.Id, tmp.IdColuna1, tmp.IdColuna2, tmp.Valor1, tmp.Valor2, tmp.DataAtualizacao 
from #tmpHistorico tmp
join #tmpHistoricoMaxData tmpDt on tmp.Id = tmpDt.Id and tmp.IdColuna1 = tmpDt.IdColuna1 and tmp.IdColuna2 = tmpDt.IdColuna2 and tmp.DataAtualizacao = tmpDt.DataAtualizacao
where tmp.Id= 1
    
10.04.2018 / 12:46