Does PostgreSQL have a pseudo-column like "LEVEL" in Oracle?
If not, then how can we create a column similar to "LEVEL"?
相关问题
- Can I skip certificate verification oracle utl_htt
- Django distinct is not working
- PostgreSQL: left outer join syntax
- Connecting Python to a Heroku PostgreSQL DB?
- PostgreSQL - Deleting data that are older than an
The functionality using a Connect By, Starts With, and a level indicator that you are familiar with is available if you enable the tablefunc extension in postgres. The syntax is slightly different, but if you understand connect by from oracle you will pick this up in about 90 seconds. It is great and saved my bacon when I transformed an oracle system into a postgres system.
I gave all the detail to a similar question.
Stackoverflow Connect By answer
Postgres does not have hierarchical queries. No
CONNECT BY
, therefore also noLEVEL
.The additional module tablefunc provides the function
connectby()
doing almost the same. See:Or you can do similar things with a standard recursive CTE and a
level
column that's incremented with every recursion.This query in Oracle:
.. can be translated to this recursive CTE in Postgres:
Yes, Postgres has support for "LEVEL" like Oracle.
But, as the other answers point out, you must have the tablefunc extension loaded.
If you have admin access to your Postgres database you can load it with this:
CREATE EXTENSION IF NOT EXISTS tablefunc;
For additional info check the docs
https://www.postgresql.org/docs/current/static/tablefunc.html
Here's a real life example of connectby from one of our apps. We are using it to find all the people who report up to a manager through their reporting tree.
And it returns results like this. Here you can see the level, and also the whole hierarchy as a string.