簡體   English   中英

選擇2個相同行之間的最大值MYSQL

[英]Select maximum value between 2 same rows MYSQL

我這里有這張桌子

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

我想要做的就是選擇每個用戶的最大值。 (如您所見,每個用戶都出現了多次。)

例如:

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

想要的結果:

FRANCESCA BIFULCO | 1 | 84

我嘗試過的:

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

注意:內部查詢返回照片中顯示的內容。

非常感謝您的幫助!

用您請求的任何表替換 yourTableName

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

架構

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查詢

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM