# Query second (n-th) last value?

**URL:** https://community.influxdata.com/t/query-second-n-th-last-value/13497
**Category:** Systems
**Created:** [March 16, 2020, 10:39am UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497 "2020-03-16T10:39:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rpiitd](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/rpiitd/32/4622_2.png) [@rpiitd](https://community.influxdata.com/u/rpiitd)
#### Post date: [March 16, 2020, 10:39am UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/1 "2020-03-16T10:39:21Z")

</div>

Is it possible to query only second (n-th) last value from a measurement?

---

<div class="post-metadata">

### Author: ![Pooh](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Pooh](https://community.influxdata.com/u/Pooh)
#### Post date: [March 16, 2020, 10:53am UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/2 "2020-03-16T10:53:49Z")

</div>

A bit ugly, but should work:

Select value from (select value from table where whatever order by time desc  
limit 2) order by time limit 1;

Antony.

---

<div class="post-metadata">

### Author: ![rpiitd](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/rpiitd/32/4622_2.png) [@rpiitd](https://community.influxdata.com/u/rpiitd)
#### Post date: [March 16, 2020, 11:31am UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/3 "2020-03-16T11:31:25Z")

</div>

> [@Pooh](#):
>
> 2

If I am running this query then I am getting an error that

> subqueries must be ordered in the same direction as the query itself

---

<div class="post-metadata">

### Author: ![Pooh](https://avatars.discourse-cdn.com/v4/letter/p/f05b48/32.png) [@Pooh](https://community.influxdata.com/u/Pooh)
#### Post date: [March 16, 2020, 11:44am UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/4 "2020-03-16T11:44:05Z")

</div>

Meh. I don’t think I have an alternative idea, then. Hopefully someone else  
has.

Antony.

---

<div class="post-metadata">

### Author: ![rpiitd](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/rpiitd/32/4622_2.png) [@rpiitd](https://community.influxdata.com/u/rpiitd)
#### Post date: [March 16, 2020, 12:26pm UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/5 "2020-03-16T12:26:51Z")

</div>

After much hit and trial, I found a way  
To get 2nd last value:  
SELECT st FROM (SELECT value as st FROM Value WHERE whatever ORDER BY time DESC LIMIT 2 OFFSET 1) ORDER BY time DESC LIMIT 1

To get 3rd last value:  
SELECT st FROM (SELECT value as st FROM Value WHERE whatever ORDER BY time DESC LIMIT 3 OFFSET 2) ORDER BY time DESC LIMIT 1

---

<div class="post-metadata">

### Author: ![phil333](https://avatars.discourse-cdn.com/v4/letter/p/5e9695/32.png) [@phil333](https://community.influxdata.com/u/phil333)
#### Post date: [February 26, 2023, 6:36pm UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/6 "2023-02-26T18:36:19Z")

</div>

any idea how to perform this with flux?

---

<div class="post-metadata">

### Author: ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)
#### Post date: [February 26, 2023, 7:01pm UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/7 "2023-02-26T19:01:29Z")

</div>

@phil333

The only way that I can guess this might be possible in Flux is if you knew how many records (rows) were being returned, and you used the limit() function. For example, if the table had 15 rows and you wanted the 13th row, you would use

```auto
|> limit(n: 13)

```

followed by the `last()` function.

---

<div class="post-metadata">

### Author: ![phil333](https://avatars.discourse-cdn.com/v4/letter/p/5e9695/32.png) [@phil333](https://community.influxdata.com/u/phil333)
#### Post date: [March 1, 2023, 1:10pm UTC](https://community.influxdata.com/t/query-second-n-th-last-value/13497/8 "2023-03-01T13:10:06Z")

</div>

Awesome @grant1 that works perffectly. These are the flux lines I used for the 4th last element

> |\> sort(columns: [“\_time”],desc:true)  
> |\> limit(n: 4)  
> |\> last()
