How to give a select and bring the top 1 of each group?

1

I have a table of processes. That is, each time a process is run, it generates a new row. I want to give a select bringing the last execution of each process. Here is an example:

Id|ProcessoNome|Situação|DataInicio
1 |RPA_001     |sucesso |03/10/2018
2 |RPA_002     |sucesso |04/10/2018
3 |RPA_003     |erro    |04/10/2018
4 |RPA_003     |sucesso |04/10/2018
5 |RPA_001     |sucesso |04/10/2018
    
asked by anonymous 04.10.2018 / 21:56

2 answers

2

Hello,

You need the last run of each process, right? Here is a query that resolves this scenario.

select distinct nome_processo,
  (select max(data)
     from processos p2
    where p1.nome_processo = p2.nome_processo) as ULTIMA_EXECUCAO
  from processos p1;

Data example:

+----+---------------+-----------------+------------+
| id | nome_processo | status_processo | data       |
+----+---------------+-----------------+------------+
|  1 | RPA_001       | sucesso         | 2018-10-03 |
|  2 | RPA_002       | sucesso         | 2018-10-04 |
|  3 | RPA_003       | erro            | 2018-10-05 |
|  4 | RPA_003       | sucesso         | 2018-10-04 |
|  5 | RPA_001       | sucesso         | 2018-10-04 |
|  6 | RPA_002       | sucesso         | 2018-10-06 |
+----+---------------+-----------------+------------+

Query return:

+---------------+------------+---+
| nome_processo | ULTIMA_EXECUCAO|
+---------------+----------------+
| RPA_001       | 2018-10-04     |
| RPA_002       | 2018-10-06     | 
| RPA_003       | 2018-10-05     | 
+---------------+----------------+

Any questions are there, good luck!

    
04.10.2018 / 22:40
0

My suggestion is you order% execution% in data .

SELECT *FROM tb_processos
ORDER BY data_inicio DESC;

The rest is ideal to deal directly with the code.

    
04.10.2018 / 22:06