postgresql - 使用多个标准对分区进行排序
问题描述
我尝试使用几个标准对分区进行排序。
使用此查询,我有以下输出:
SELECT id, aggregate_id, aggregate_update, detection_time
FROM report.report_table R1
WHERE table_name NOT LIKE 'AGGREGATE_ALERT_EVENT'
ORDER BY MAX(aggregate_update)
OVER (PARTITION BY aggregate_id) ASC,
aggregate_id, aggregate_update asc, detection_time desc;
我们看到行是由aggregate_id 分区的。在每个分区内,行首先按aggregate_update ASC 排序,然后按detection_time DESC 排序。但是,分区仅按 MAX(aggregate_update) 排序,我希望分区按 MAX(aggregate_update) 和 MAX(detection_time) DESC 排序。我尝试得到以下结果:
如何使用多个标准对分区本身进行排序?
解决方案
我认为这应该给你你想要的行为:
SELECT id, aggregate_id, aggregate_update, detection_time
FROM report.report_table R1
WHERE table_name NOT LIKE 'AGGREGATE_ALERT_EVENT'
ORDER BY
MAX(aggregate_update) OVER (PARTITION BY aggregate_id),
MAX(detection_time) OVER (PARTITION BY aggregate_id) DESC,
aggregate_id,
detection_time DESC;
aggregate_id
如果两组碰巧具有相同的最大更新值,我添加的第二个排序条件会打破平局。在这种情况下,排序会退回到具有较长检测时间的组来决定哪个组先来。
推荐阅读
- string - 用分号替换powerscript中字符串中的多个空格
- python - 在 python 中用drawow绘图
- c# - C#在许多看起来像列表的列表中选择一些列表
- python - 如何让我的数字猜谜游戏接受多个输入?
- python - Python -- 打开 xml 文件,根据 csv 中的数据更改数据,另存为新的 xml 文件
- javascript - 将 request.js 转换为 axios (Vonage api)
- python - 如何在 django 模板中迭代 dict 列表?
- python - 模型中数组的输入数据格式问题
- python - 如何将 else 语句添加到 dict 理解中?
- c++ - 程序在带有 3 个变量 C++ 的 while 循环中保持循环