简体   繁体   中英

Select maximum value between 2 same rows MYSQL

I have this table here

CONCAT(Nome,'',Cognome) Interno value
FRANCESCA BIFULCO       1           0
FRANCESCA BIFULCO       1          84
FRANCESCA BIFULCO       1A        570
FRANCESCA BIFULCO       1A        972
RICCIARDELLI            2        1276
RICCIARDELLI            2        1320

All I want to do is to select the maximum value per User. ( As you saw, each user appears multiple times.)

For Example:

FRANCESCA BIFULCO | 1 | 0
FRANCESCA BIFULCO | 1 | 84

Result wanted:

FRANCESCA BIFULCO | 1 | 84

What I have tried:

select a.ut, max(value)
from (
select Utenti_Condomini.ID_Condominio,CONCAT(Nome,' ', Cognome) as ut, Utenti_Condomini.Interno as i, Greatest(Max(Val_Primo), Max(Val_Secondo), Max(Val_Terzo), Max(Val_Quarto) )as value 
from Letture_Acqua, Utenti_Condomini 
where ID = 19 
and Utente = CONCAT(Nome,' ', Cognome)
and ID = ID_Condominio and Interno = Internus 
group by Utente, Internus, Anno 
order by id_user+0
)a 
group by a.ut, a.i

NOTE: The inner query returns what it is shown in the photo.

Thank you very much for your help!

用您请求的任何表替换 yourTableName

SELECT CONCAT(Nome,' ',Cognome),interno,MAX(value) FROM yourTableName GROUP BY Nome,Cognome,interno;

Schema

create table test(
fullname varchar(100),
category char(5),
value int
);
insert into test values("TATA BIRLA", "1",0);
insert into test values("TATA BIRLA", "1",80);
insert into test values("TATA BIRLA", "1A",570);
insert into test values("TATA BIRLA", "1A",972);

SQL Query

SELECT fullname, max(value)
from test
group by fullname,category;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM