2

I have the following query in oracle. I want to convert it to PostgreSQL form. Could someone help me out in this,

SELECT user_id, user_name, reports_to, position 
FROM   pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;
2
  • You'll probably have to show some sample data, expected results, and/or table definitions. In general, look at WITH RECURSIVE and common table expressions. Commented Jun 4, 2014 at 13:38
  • What is your PostgreSQL version? Commented Jun 5, 2014 at 7:40

1 Answer 1

5

A something like this should work for you (SQL Fiddle):

WITH RECURSIVE q AS (
    SELECT po.user_id,po.user_name,po.reports_to,po.position
      FROM pr_operators po
     WHERE po.reports_to = 'dpercival'
    UNION ALL
    SELECT po.user_id,po.user_name,po.reports_to,po.position
      FROM pr_operators po
      JOIN q ON q.user_id=po.reports_to
)
SELECT * FROM q;

You can read more on recursive CTE's in the docs.

Note: your design looks strange -- reports_to contains string literals, yet it is being comapred with user_id which typicaly is of type integer.

Sign up to request clarification or add additional context in comments.

1 Comment

This query doesn't work. It is giving the following error ERROR: syntax error at or near "SELECT"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.