Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last N rows in the database in order?

Tags:

postgresql

Let's say I have the following database table:

 record_id | record_date | record_value -----------+-------------+--------------          1 | 2010-05-01  |       195.00          2 | 2010-07-01  |       185.00          3 | 2010-09-01  |       175.00          4 | 2010-05-01  |       189.00          5 | 2010-06-01  |       185.00          6 | 2010-07-01  |       180.00          7 | 2010-08-01  |       175.00          8 | 2010-09-01  |       170.00          9 | 2010-10-01  |       165.00 

I want to grab the last 5 rows with the data ordered by record_date ASC. This is easy to do with:

SELECT * FROM mytable ORDER BY record_date ASC LIMIT 5 OFFSET 4 

Which would give me:

 record_id | record_date | record_value -----------+-------------+--------------          6 | 2010-07-01  |       180.00          7 | 2010-08-01  |       175.00          3 | 2010-09-01  |       175.00          8 | 2010-09-01  |       170.00          9 | 2010-10-01  |       165.00 

But how do I do this when I don't know how many records there are and can't compute the magic number of 4?

I've tried this query, but if there are less than 5 records, it results in a negative OFFSET, which is invalid:

SELECT * FROM mytable ORDER BY record_date ASC LIMIT 5      OFFSET (SELECT COUNT(*) FROM mytable) - 5; 

So how do I accomplish this?

like image 588
Kristopher Avatar asked Apr 08 '10 02:04

Kristopher


People also ask

How do I get last N rows in SQL?

mysql> SELECT * FROM ( -> SELECT * FROM Last10RecordsDemo ORDER BY id DESC LIMIT 10 -> )Var1 -> -> ORDER BY id ASC; The following is the output that displays the last 10 records.

How do I get last 5 rows in SQL?

METHOD 1 : Using LIMIT clause in descending order As we know that LIMIT clause gives the no. of specified rows from specifies row. We will retrieve last 5 rows in descending order using LIMIT and ORDER BY clauses and finally make the resultant rows ascending.

How do I get the last 3 rows of a SQL table?

Try only this:- SELECT * FROM reset ORDER BY ASC LIMIT (FOUND_ROWS() - 3), 3 and check if it is giving the last 3 rows from your table in ascending order!!!

How do I select the last two rows in SQL?

To select last two rows, use ORDER BY DESC LIMIT 2.


2 Answers

Why don't you just order the opposite way?

SELECT * FROM mytable ORDER BY record_date DESC LIMIT 5; 

If you don't want to flip back correctly in the application, you can nest a query and flip them twice:

SELECT *     FROM (SELECT * FROM mytable ORDER BY record_date DESC LIMIT 5)     ORDER BY record_date ASC; 

...which turns out to be a pretty cheap operation.

like image 175
Travis Gockel Avatar answered Sep 26 '22 17:09

Travis Gockel


This should work:

WITH t AS (     SELECT * FROM mytable ORDER BY record_date DESC LIMIT 5 ) SELECT * FROM t ORDER BY record_date ASC; 
like image 42
Zaza Zviadadze Avatar answered Sep 26 '22 17:09

Zaza Zviadadze