PostgresSQL get last 20 rows and then get max value

Posted on 2014-08-19
Last Modified: 2014-08-19

I have a table that I need to list the last 20 rows from end_time and then base off that get the max value of the column run_time.


select run_time_sec,end_time FROM runinfo_history
where sched_table = 'TEST_TIMES'
  and job_mem_name = 'TEST_TIMES'
  and data_center = 'DEV'
order BY end_time DESC LIMIT 20

gets the last 20

run_time_sec     end_time            
 ---------------  --------------------
 4                8/19/2014 6:25:39 PM
 4                8/19/2014 6:25:07 PM
 4                8/19/2014 6:24:29 PM
 3                8/19/2014 6:23:40 PM
 3                8/19/2014 6:23:11 PM
 3                8/19/2014 6:23:01 PM
 4                8/19/2014 6:22:51 PM
 3                8/19/2014 6:22:41 PM
 4                8/19/2014 6:22:32 PM
 4                8/19/2014 6:22:21 PM
 4                8/19/2014 6:21:32 PM
 4                8/19/2014 6:21:22 PM
 4                8/19/2014 6:21:12 PM
 4                8/19/2014 6:20:52 PM
 4                8/19/2014 6:20:42 PM
 4                8/19/2014 6:20:32 PM
 3                8/19/2014 6:20:22 PM
 3                8/19/2014 6:20:02 PM
 3                8/19/2014 6:19:50 PM
 60               8/19/2014 6:11:13 PM

I do not need the column end_time. Its just hear as to show what it is but used to get the last 20 dates from that column.

Not sure who to combine to get max from the list.


Question by:mikeysmailbox1
    LVL 11

    Accepted Solution

    You can get the max run_time_sec from your query with something like:

    select max(run_time_sec) from (
      select run_time_sec FROM runinfo_history
        where sched_table = 'TEST_TIMES'
          and job_mem_name = 'TEST_TIMES'
          and data_center = 'DEV'
          order BY end_time DESC LIMIT 20
    ) as t1;

    Open in new window


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: ( This requires some add-o…
    Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now