【问题标题】:Select maximum value between 2 same rows MYSQL选择2个相同行之间的最大值MYSQL
【发布时间】:2018-03-02 15:49:37
【问题描述】:

我这里有这张桌子

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

注意: 内部查询返回照片中显示的内容。

非常感谢您的帮助!

【问题讨论】:

标签: mysql select max rows


【解决方案1】:

将 yourTableName 替换为您请求的任何表

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

【讨论】:

  • 谢谢先生。 DJ,我编辑了这个问题,因为我已经尝试过了
【解决方案2】:

架构

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;

【讨论】:

  • 虽然这适用于单个表,但我发现它不适用于连接。也许我错过了一些参考完整性规则。
  • 获取具有相同列集的所有行,并按 group by 子句包装。示例: select x.fullname, max(x.value) from ( select fullname, value,category from test union select 'IMS' as fullname, 90 as value, '1B' as category from dual ) x group by x.fullname, x.category;
猜你喜欢
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多