gpt4 book ai didi

java - 我的查询需要很长时间才能完成查找列值差异为另一列最大分组的行对

转载 作者:行者123 更新时间:2023-11-30 05:24:47 26 4
gpt4 key购买 nike

比如说,我有一张这样的 table :

enter image description here

我想找到每次 session 中性能差异最大的一对中心,如下所示:

enter image description here

我有以下查询,

select 
t1.session,
t1.center center1,
t2.center center2,
t1.performance - t2.performance performance
from mytable t1
inner join mytable t2 on t1.session = t2.session
where t1.performance - t2.performance = (
select max(t11.performance - t22.performance)
from mytable t11
inner join mytable t22 on t11.session = t22.session
where t11.session = t1.session
)

它可以工作,但对于 20 列和 200 行的表来说需要很长时间,几分钟。 如何修改查询以更快地实现相同的输出?

最佳答案

select 
t1.session,
t1.center center1,
t2.center center2,
t1.performance - t2.performance performance
from mytable t1
inner join mytable t2
on t1.session = t2.session
WHERE t1.performance = (SELECT MAX(performance)
FROM mytable t3 WHERE t3.session = t1.session)
AND t2.performance = (SELECT MIN(performance)
FROM mytable t3 WHERE t3.session = t2.session)

// Im thinking this will solve the border case when performance is a tie
// and difference 0 will return 2 rows

AND (CASE WHEN t1.performance = t2.performance
THEN CASE WHEN t1.center < t2.center
THEN 1
ELSE 0
END
ELSE 1
END) = 1

只要您有关于性能 session 的索引就应该没问题。

关于java - 我的查询需要很长时间才能完成查找列值差异为另一列最大分组的行对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58879412/

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