Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6553967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:38:40+00:00 2026-05-25T12:38:40+00:00

I have some Order s created by Customers stored in postgres db.Some of the

  • 0

I have some Orders created by Customers stored in postgres db.Some of the Orders may have 'pending' status.I want the user to be able to work on his last pending Order.

order table

id   | order_num | order_date |customer_id | status
-----+-----------+------------+------------+---------
80   | 1234      | 02-01-2000 | 20         | pending
----------------------------------------------------
81   | 2345      | 02-01-2000 |20          | confirmed
-----------------------------------------------------
82   | 3456      | 02-01-2000 |20          | pending
--------------------------------------------------
83   | 3498      | 02-001-2000|  20        | confirmed
----------------------------------------------------

Is there anything wrong with using

select * from orders where customer_id =20 and status='pending' order by id DESC limit 1

The order_date for multiple records may be same.The order number may not always be in ascending order. Is ordering by id safe in this regards?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:38:41+00:00Added an answer on May 25, 2026 at 12:38 pm

    You should order by the order_date with id as a secondary sort key:

    select *
    from orders
    where customer_id = 20
      and status      = 'pending'
    order by order_date desc, id desc
    limit 1
    

    You want the most recent order so you should sort to express that intent. You also have to deal with duplicate order_date values so you can throw id in as a secondary sort key to, hopefully, get the most recent one on the most recent order_date. This assumes that id is a serial or bigserial column.

    You’re probably safe using order by id desc as long as the order_date is not allowed to change after the record is created. However, if you order by order_date desc, id desc then it will be immediately obvious what your intent is, the people maintaining your code (possibly you) might appreciate it when you saying exactly what you mean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem to order an array by a field of this, here
I seems to have some issues in order to compile gproc from uwiger. I
I have some doubts about construction and initialization order guarantees in C++. For instance,
I have some things in my app that I need to store, in order
I have some types with a sequence inside, which does restrict the order of
I have a hashtable . values() method returns values in some order different from
I'm trying to save a file in which I have some credentials. In order
I have written some code to ensure that items on an order are all
So I have a class is something like this public class Order { //some
I have next tables Order , Transaction , Payment . Class Order has some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.