sql - Psql 查询:联合不保留来自 cte 的行的顺序
问题描述
我有这个查询:
with cte1 as (select salary from employees order by salary desc), cte2 as (select 850) select * from cte1 union select * from cte2;
cte1 按工资列排序,但 cte2 不是。我希望将 cte2 的结果附加到 cte1 的结果中,同时保留 cte1 的结果顺序。但它没有发生。
如果我在没有与第二个 cte 联合的情况下运行上述查询,结果将以预期的顺序显示,但是当联合存在时顺序会变得混乱。
没有联合的查询:
with cte1 as (select salary from employees order by salary desc), cte2 as (select 850) select * from cte1;
salary
--------
1000
900
800
700
600
500
与工会:
with cte1 as (select salary from employees order by salary desc), cte2 as (select 850) select * from cte1 union select * from cte2;
salary
--------
850
800
700
900
500
600
1000
谁能解释为什么会这样?
解决方案
文档很清楚,union
不能保证行的顺序:
UNION
有效地将 query2 的结果附加到 query1 的结果(尽管不能保证这是实际返回行的顺序)。
如果要对结果进行排序,则order by
在外部查询中使用;对于您的用例,这需要跟踪每行来自哪个 cte
with
cte1 (salary, which) as (select salary, 1 from employees),
cte2 (salary, which) as (select 850, 2)
select salary from cte1
union all
select salary from cte2
order by which, salary desc;
请注意,我更改union
为union all
; 您似乎不想对行进行重复数据删除(前者会这样做),因此后者已经足够好(而且效率更高)。
推荐阅读
- reactjs - React Hooks & useContext:这是一个好的模式吗?
- python - 由于我的循环执行 100000 次的特定测试用例超时,我被终止了。谁能帮我解决这个问题?
- ios - 在构建应用程序的第一个版本时,是否有标准使用的特定 iOS 设备尺寸?
- java - Android - 如何使用 getId() 访问特定于方向的布局子视图
- reactjs - 如何使用 react-testing-library 在 react-select 组件上触发更改事件?
- regex - 正则表达式从组值中选择第二个选项(a | b)
- java - 你如何在 Java 中绘制一个形状下的另一个形状?
- node.js - 使用 Express js 订阅/发布
- scala - 一定时间后如何不断增加值?
- javascript - 使用嵌套数组从对象中删除项目