gpt4 book ai didi

java - 如何从表中查找最大值和最小值以及另一列值?

转载 作者:行者123 更新时间:2023-12-01 19:33:17 25 4
gpt4 key购买 nike

我有一个这样的表:

enter image description here

我想找到相应中心名称的最大和最小效率,按 session 分组 - 像这样:

enter image description here

我试过了

Select max(Efficiency) as max_Efficiency, Center as max_Center, 
min(Efficiency) as min_Efficiency, Center as min_Center
from myTable
group by session

但它不起作用,出现错误。如果我离开中心(两种情况),它可以工作,但我也需要中心名称。

最佳答案

你可以尝试这样的事情:

select session, max_efficiency, max_center, min_Efficiency, min_center from (
select tbl.*,
case when tbl.session = nt.session and tbl.max_efficiency = Efficiency then nt.center end max_center,
case when tbl.session = nt.session and tbl.max_efficiency = Efficiency then nt.center end min_center
from (
select session,
max(Efficiency) max_Efficiency,
min(Efficiency) min_Efficiency
from myTable
group by session
) tbl
join myTable mt
on mt.session = tbl.session
) where (max_center is not null and min_center is not null)

如果您的表有大量数据,我不确定执行时间,但这应该可以解决问题。

可能有一种更简单的方法来减少这个查询。

关于java - 如何从表中查找最大值和最小值以及另一列值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58820462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com