我有这个查询(已经精简但实际上更复杂):
SELECT
e.id,
string_agg(csb.description, ',' ORDER BY cs.id ASC) AS condition_set_descriptions
FROM event e
JOIN event_trigger_version etv ON e.event_trigger_version_id = etv.id
LEFT JOIN event_condition_set ecs ON ecs.event_id = e.id
JOIN condition_set cs ON cs.id = ecs.condition_set_id
JOIN condition_set_base csb ON cs.condition_set_base_id = csb.id
JOIN subject s ON e.subject_id = s.id
GROUP BY
e.id, event_level, s.name
ORDER BY s.name ASC, condition_set_descriptions ASC, event_level DESC
LIMIT 20 OFFSET 0
现在我也可以有一个动态的ORDER BY
包括其他列(在这个例子中省略了),但总是包括condition_set_descriptions
顺序中的某个地方。此列是string_agg
函数的结果。我不能将它移动到子查询,因为LIMIT
设置的应该适用于定义的列组合的结果。ORDER BY
该示例工作正常,但缺点是该condition_set_descriptions
列也作为查询的结果返回,但这是很多数据并且实际上并不需要(因为使用省略的一些以另一种方式查找实际描述数据)。所需要的只是对结果进行排序。如果不在子查询中选择它会破坏多排序受限结果集的正确性,我该如何做到这一点?