sql - 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 可以是空字符串 -''
解决方案
这是一种方法:
SELECT col1, col2,max(abs(col1 - col2))
from test1
group by col1,col2
order by max(abs(col1 - col2)) desc
LIMIT 1
如果不清楚,请告诉我。
推荐阅读
- react-native-android - 在反应本机应用程序中弹出后刷新android资产
- php - 找不到cron命令-CentOS 7
- go - 在 golang 程序中运行交互式 shell 脚本
- reactjs - 带钩子的 useEffect 给了我一个警告信息
- html - 如何将我的文本调整为我的屏幕尺寸,并固定位置
- ios - 将 UITableViewController 转换为 SwiftUI 后,UITableViewCell 中的视图为零
- php - 如何从php中的字符串数组中获取数组?
- python - 如何从使用 Python 返回文本的 API 将文本解析为 json
- python - 如何在 APScheduler 中即时修改作业?
- c++ - 如何在 C++ 中进行线程安全的光线投射?