Fórum SQL: CONSULTA COM MAX AGRUPADOS POR ID #620849

14/12/2023

0

Olá, boa tarde!


Preciso de ajuda para montar o seguinte select:

Cenário:
Tabela:
INDICE ID C_C PERC
1 123 4521 60
2 123 4221 40
3 124 8565 33
4 124 1425 33
5 124 2365 34
6 125 2555 100

Preciso retornar na consulta o Indice, ID e C_C baseado no maior PERC por ID.
Nesse exemplo, o resultado seria:

INDICE ID C_C PERC
1 123 4521 60
5 124 2365 34
6 125 2555 100

BD: Oracle

Agradeço desde já!
Vanessa Oliveira

Vanessa Oliveira

Responder

Posts

15/12/2023

Arthur Heinrich

1
2
3
4
5
6
7
8
9
select indice, id, c_c, perc
from
 (select
    indice, id, c_c, perc,
    row_number() over(partition by id order by perc desc, id) seq
  from tabela) t
where
  seq = 1
order by id

Responder

Gostei + 0

15/12/2023

Arthur Heinrich

1
2
3
4
5
6
7
8
9
select indice, id, c_c, perc
from
 (select
    indice, id, c_c, perc,
    row_number() over(partition by id order by perc desc, indice) seq
  from tabela) t
where
  seq = 1
order by id

Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar