RE: How to just get the last in a recursive query

From: "Tchouante, Merlin" <mtchouan(at)umaryland(dot)edu>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>, Rob Sargent <robjsargent(at)gmail(dot)com>
Cc: "pgsql-sql(at)lists(dot)postgresql(dot)org" <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: RE: How to just get the last in a recursive query
Date: 2022-04-05 13:03:44
Message-ID: DM4PR12MB51193AB0FFB19CEDAAF997DBD2E49@DM4PR12MB5119.namprd12.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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(at)umaryland(dot)edu<mailto:mtchouan(at)umaryland(dot)edu>
410-706-4489 * 410-706-1500 fax

Please send Blackboard questions to the CITS support email address: DL-CITSBbSupport(at)umaryland(dot)edu<mailto:dl-citsbbsupport(at)umaryland(dot)edu>
Please send Mediasite questions to the CITS support email address: DL-CITSMediasiteSupport(at)umaryland(dot)edu<mailto:DL-CITSMediasiteSupport(at)umaryland(dot)edu>

[New UMB Logo]

From: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Sent: Monday, April 4, 2022 7:22 PM
To: Rob Sargent <robjsargent(at)gmail(dot)com>
Cc: pgsql-sql(at)lists(dot)postgresql(dot)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(at)gmail(dot)com<mailto:robjsargent(at)gmail(dot)com>> wrote:
On 4/4/22 16:14, Shaozhong SHI wrote:

---------- Forwarded message ---------
From: Shaozhong SHI <shishaozhong(at)gmail(dot)com<mailto:shishaozhong(at)gmail(dot)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(at)lists(dot)osgeo(dot)org<mailto:postgis-users(at)lists(dot)osgeo(dot)org>>

In this example, Network Walking in PostGIS * Paul Ramsey (cleverelephant.ca)<https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fblog.cleverelephant.ca%2F2010%2F07%2Fnetwork-walking-in-postgis.html&data=04%7C01%7Cmtchouan%40umaryland.edu%7Cccca8e4f951e4780dde008da1691ecb0%7C3dcdbc4a7e4c407b80f77fb6757182f2%7C0%7C0%7C637847113257889566%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=xVii1WKKxTDHx5kZUH49X5tyWwbPkxanjSBoMgpDnCk%3D&reserved=0>

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jian He 2022-04-05 14:34:44 Histogram question.
Previous Message David G. Johnston 2022-04-05 00:50:09 Re: How to just get the last in a recursive query