我需要将查询从Oracle SQL转换为Postgres。
var blob = new Blob([str], {type: "application/octet-stream;charset=UTF-8,"});
如果我用“row_number()over()”替换“rownum”,我会收到一条错误消息:“HAVING中不允许使用窗口函数”。 你可以帮我在Postgres中获得与Oracle相同的结果吗?
答案 0 :(得分:1)
下面的查询将执行Oracle查询正在执行的操作。
select count(*) from
(select column1, row_number() over () as x from table1) as t
group by column1 having max(t.x) = 6;
<强>然而强>
除非您指定order by子句,否则oracle不会postgres将保证读取记录的顺序。因此,根据数据库决定处理查询的方式,多次运行查询将不一致。当然在postgres中,任何更新都会改变基础行顺序。
在下面的例子中,我有一个seq
的额外列,用于提供一致的排序。
CREATE TABLE table1 (column1 int, seq int);
insert into table1 values (0,1),(0,2),(0,3),(1,4),(0,5),(1,6);
一个强制订单一致的修订查询:
select count(*) from
(select column1, row_number() over (order by seq) as x from table1) as t
group by column1 having max(t.x) = 6;