How to generate the order if you know the next item for each item?

From: Rockdale Green <rockdale(dot)green(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: How to generate the order if you know the next item for each item?
Date: 2021-08-03 15:40:03
Message-ID: CAArxxWj=_gc=6RL0QGPzpbXMLZTvxDOqG-sQVxbpCmVu5Q2jpQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi, all:

I have two tables one is a lookup for all the tasks, another table stores
the next task for each task, I want to generate a list of tasks with its
sequence number, can I do it in one query?

Table task

id name
1 postoffice
2 grocery store
3. walk dog
4. swimming
5 roller skate
7 biking

table next_task

task next_task
3 7
7 1
1 4
4 2
2 5

and I want to get the list in order

task task_order
3 (walk dog) 1
7 (biking) 2
1 (postoffice) 3
4 (swimming) 4
2 (growcery store) 5
5 (roller skate) 6

It might be just very simple and I have tried but somehow I am blocked.

Thanks in advance
-Rockdale

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2021-08-03 16:22:19 Re: How to generate the order if you know the next item for each item?
Previous Message Steve Midgley 2021-07-29 19:43:02 Re: Equivalent of InternalRowsAffected in NonExecuteQuery for PostgreSQL