Aid in SELECT DISTINCT

Asked

Viewed 485 times

4

I have the following select:

SELECT CD_CLIENTE, 
       DT_ATUALIZACAO,  
  FROM TABELA

Return me the following result:

inserir a descrição da imagem aqui

But I’d like to select just the last DT_ATUALIZAÇÃO of each customer, I thought to use a SELECT DISTINCT but it won’t work. You can help me?

3 answers

6

Use the form below:

SELECT COD_CLIENTE,MAX(DT_ATUALIZACAO) FROM TABELA GROUP BY COD_CLIENTE;

Depending on the situation, use MIN() instead of MAX();

I hope I’ve helped! :)

4


I thought the following, as you want the latest update according to the date so you want the highest value date.

So I did the same SELECT but I added a clause WHEREwho uses the function MAX() to fetch the latest date:

SELECT CD_CLIENTE, 
       DT_ATUALIZACAO,  
FROM 
   TBL_CLIENTES_SOFTWARE_HOUSE_ATUALIZACAO  t1
WHERE 
    t1.DT_ATUALIZACAO = (SELECT MAX(t2.DT_ATUALIZACAO) FROM TBL_CLIENTES_SOFTWARE_HOUSE_ATUALIZACAO  t2 WHERE t2.CD_CLIENTE = t1.CD_CLIENTE)

2

Using SELECT DISTINCT you will return only a single value for each customer(CD_CLIENTE) if it happens again, even then, would not solve your problem.

How you want to recover the client code along with the larger one(MAX()) update date. You can recover as follows.

SELECT CD_CLIENTE, 
       MAX(DT_ATUALIZACAO) 
FROM TABELA GROUP BY CD_CLIENTE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.