Thread: How to just get the last in a recursive query
From: Shaozhong SHI <shishaozhong@gmail.com>
Date: Mon, 4 Apr 2022 at 23:13
Subject: How to just get the last in a recursive query
To: PostGIS Users Discussion <postgis-users@lists.osgeo.org>
id
--- 6 3 1
How to just get the last (namely, 1) in the most efficient way?
Regards, David
reverse the order of the last query and set limit 1---------- Forwarded message ---------
From: Shaozhong SHI <shishaozhong@gmail.com>
Date: Mon, 4 Apr 2022 at 23:13
Subject: How to just get the last in a recursive query
To: PostGIS Users Discussion <postgis-users@lists.osgeo.org>In this example, Network Walking in PostGIS · Paul Ramsey (cleverelephant.ca)3 rows got returns as follows:id --- 6 3 1
How to just get the last (namely, 1) in the most efficient way?
Regards, David
On Monday, 4 April 2022, Rob Sargent <robjsargent@gmail.com> wrote:
On 4/4/22 16:14, Shaozhong SHI wrote:reverse the order of the last query and set limit 1---------- Forwarded message ---------
From: Shaozhong SHI <shishaozhong@gmail.com>
Date: Mon, 4 Apr 2022 at 23:13
Subject: How to just get the last in a recursive query
To: PostGIS Users Discussion <postgis-users@lists.osgeo.org> In this example, Network Walking in PostGIS · Paul Ramsey (cleverelephant.ca)3 rows got returns as follows:id --- 6 3 1
How to just get the last (namely, 1) in the most efficient way?
Regards, David
That is not the most efficient in this case.Don't top postHow to tell query to deliberately miss out all except the last one is of interest.Regards, David
On Monday, 4 April 2022, Rob Sargent <robjsargent@gmail.com> wrote:On 4/4/22 16:14, Shaozhong SHI wrote:reverse the order of the last query and set limit 1---------- Forwarded message ---------
From: Shaozhong SHI <shishaozhong@gmail.com>
Date: Mon, 4 Apr 2022 at 23:13
Subject: How to just get the last in a recursive query
To: PostGIS Users Discussion <postgis-users@lists.osgeo.org> In this example, Network Walking in PostGIS · Paul Ramsey (cleverelephant.ca)3 rows got returns as follows:id --- 6 3 1
How to just get the last (namely, 1) in the most efficient way?
Regards, David
And your definition of "last" is what, exactly
That is not the most efficient in this case.
On Mon, Apr 4, 2022, 16:21 Shaozhong SHI <shishaozhong@gmail.com> wrote:That is not the most efficient in this case.Can you prove that statement? Provide a query that is more efficient.
WITH RECURSIVE walk_network(id, segment) AS ( SELECT id, segment FROM network WHERE id = 6 UNION ALL SELECT n.id, n.segment FROM network n, walk_network w WHERE ST_DWithin( ST_EndPoint(w.segment), ST_StartPoint(n.segment),0.01)
)
SELECT id
FROM walk_network
On Mon, Apr 4, 2022 at 4:32 PM David G. Johnston <david.g.johnston@gmail.com> wrote:On Mon, Apr 4, 2022, 16:21 Shaozhong SHI <shishaozhong@gmail.com> wrote:That is not the most efficient in this case.Can you prove that statement? Provide a query that is more efficient.Just to share the SQL from that exampleWITH RECURSIVE walk_network(id, segment) AS ( SELECT id, segment FROM network WHERE id = 6 UNION ALL SELECT n.id, n.segment FROM network n, walk_network w WHERE ST_DWithin( ST_EndPoint(w.segment), ST_StartPoint(n.segment),0.01) ) SELECT id FROM walk_network
David J (kind of off-topic): There's no order by in the original query, so I could imagine that adding any order by clause at all would make the query less efficient. But maybe it could become more efficient if the planner picks a better index as a result?David (OP): My main point is that in this example, since no order by clause is provided, it is meaningless to talk about a "last" or "first" item. SQL, afaik, is not required to produce the results in any order whatsoever, when no order by clause is provided (corrections welcome if that's not accurate). So while you might grab the last item somehow this time, it might not be the last item, the next time you run the query. So I'd say you should add an appropriate order by query, and then you can measure "ASC" vs "DESC" with "LIMIT 1" to see if either one is less efficient. (I'm in David J's camp that it's unlikely to make any difference)
These worked for me:
These lists the results in reverse order based on the rownum and returns first row, which is actually the last row, just in reversed order.
Oracle:
select *
from (select umab.umab_directory_info.*, rownum
from umab.umab_directory_info
where pidm = 0
ORDER BY ROWNUM DESC)
WHERE ROWNUM=1;
Pgsql-sql:
select course_id from (select course_main.course_id, row_number() OVER (ORDER BY course_id)
from course_main
where course_name like '%DO NOT USE%'
ORDER BY row_number DESC limit 1) cm
You need an alias (cm), otherwise you will get an error.
Thanks,
-- Merlin
Merlin D. Tchouante, Sr. IT Enterprise Application Developer
Center for Information Technology Services (CITS)
601 West Lombard Street
Baltimore, Maryland 21201-1512
mtchouan@umaryland.edu
410-706-4489 * 410-706-1500 fax
Please send Blackboard questions to the CITS support email address: DL-CITSBbSupport@umaryland.edu
Please send Mediasite questions to the CITS support email address: DL-CITSMediasiteSupport@umaryland.edu
From: Shaozhong SHI <shishaozhong@gmail.com>
Sent: Monday, April 4, 2022 7:22 PM
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-sql@lists.postgresql.org
Subject: Re: How to just get the last in a recursive query
CAUTION: This message originated from a non-UMB email system. Hover over any links before clicking and use caution opening attachments.
That is not the most efficient in this case.
How to tell query to deliberately miss out all except the last one is of interest.
Regards, David
On Monday, 4 April 2022, Rob Sargent <robjsargent@gmail.com> wrote:
On 4/4/22 16:14, Shaozhong SHI wrote:
---------- Forwarded message ---------
From: Shaozhong SHI <shishaozhong@gmail.com>
Date: Mon, 4 Apr 2022 at 23:13
Subject: How to just get the last in a recursive query
To: PostGIS Users Discussion <postgis-users@lists.osgeo.org>
In this example, Network Walking in PostGIS · Paul Ramsey (cleverelephant.ca)
3 rows got returns as follows:
id
---
6
3
1
How to just get the last (namely, 1) in the most efficient way?
Regards, David
reverse the order of the last query and set limit 1