Get registration with larger date / time

Dúvidas, dicas e truques de SQL, Select, Update, Delete, cláusulas, operações com joins, Funções em SQLs, etc
Post Reply
jerry-sc
Rank: Programador Sênior
Rank: Programador Sênior
Posts: 45
Joined: Tue, 01 Nov 2011 1:24 pm

Good morning,

I have a table, with the following structure: request, itempedo, dt_diver, dt_ultima_alt
in this table, I have several records that are "Duplicates" except, the DT_ULTIMA_ALT field. I need to get a single record of these, with the larger date / time that is recorded in the dt_ultima_alt field.

Does anyone have a notion of how to do this? I tried to bring all the records with Max, however, as has duplicate records, it brings more than one record.

Thanks.
spernega
Rank: DBA Pleno
Rank: DBA Pleno
Posts: 232
Joined: Tue, 19 Jun 2007 2:12 pm
Location: São Paulo - SP

Good morning Jerry,

Try something more or less like this:

Select all

select t.PEDIDO, t.ITEMPEDIDO, t.DT_DIVER, t.DT_ULTIMA_AL 
from   sua_tabela t 
where  DT_ULTIMA_AL = (select max(s.DT_ULTIMA_AL) 
                       from   sua_tabela s 
                       where  s.PEDIDO = t.PEDIDO 
                       and    s.ITEMPEDIDO = t.ITEMPEDIDO 
                       and    s.DT_DIVER = t.DT_DIVER);
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 14 guests