【问题标题】:How to convert the the oracle query to PostgreSQL to get the count of table如何将 oracle 查询转换为 PostgreSQL 以获取表的计数
【发布时间】:2017-01-05 13:04:12
【问题描述】:

我想将上面提到的 oracle 查询转换为 PostgreSQL 以获取表的计数

select count(*) from student connect by prior std_id=std_roll

【问题讨论】:

  • 您的 Oracle 查询没有意义。 connect by 没有 start with 似乎是错误的。 with start with 基本上与select count(*) from student 相同
  • stackoverflow.com/q/41302331/2235885 由同一作者完全相同。

标签: sql postgresql hierarchical-data hierarchical-query


【解决方案1】:

Oracle 的 connect by 可以在标准 SQL(和 Postgres)中重写为递归 common table expression

with recursive tree as (
  select std_id, std_roll
  from student
  where std_roll is null --<< I assume you are missing a START WITH in Oracle
  union all
  select c.std_id, c.std_roll
  from student c
    join tree p on p.std_id = c.std_roll
)
select count(*)
from tree;

然而,这并没有真正的意义。以上是select count(*) from student的复杂写法(假设std_rollstd_id之间有外键)

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 2017-05-09
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多