SQL: CONSULTA COM MAX AGRUPADOS POR ID
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á!
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
Curtidas 0
Respostas
Arthur Heinrich
14/12/2023
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
GOSTEI 0
Arthur Heinrich
14/12/2023
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
GOSTEI 0