首页 > 解决方案 > PostgreSQL获取Col1,Col2时它们的差异最大

问题描述

请保持您的解释初学者友好。

我想获取 max(col2-col1) 并获取我从中获得最大差异的行的 col1、col2。我怎样才能做到这一点?请提供示例代码的解释。

我的代码现在有点像下面

select
   ......
   col1,  --It should be from the row from which we got the max
   col2,  --It should be from the row from which we got the max
   max(col2-col1)
   .....
joins
   ....
where
   conditions.....
group by
   reqColumns.....

pastebin中的完整代码。Col1、Col2 和 max() 分别为 c17、c18 和 c19。另一件事是 col1 和 col2 都是时间表示为字符串 YYYYMMDDHH24MISS

谢谢提前!!!

PS: Col2 可以是空字符串 -''

标签: sqldatabasepostgresql

解决方案


这是一种方法:

SELECT col1, col2,max(abs(col1 - col2))
from test1
group by col1,col2
order by max(abs(col1 - col2)) desc
LIMIT 1

如果不清楚,请告诉我。


推荐阅读