# Sql help, how to apply conditions to lead() over partition

**URL:** <https://forum.inductiveautomation.com/t/sql-help-how-to-apply-conditions-to-lead-over-partition/59000>\
**Category:** Ignition\
**Created:** [April 28, 2022, 3:13pm UTC](https://forum.inductiveautomation.com/t/sql-help-how-to-apply-conditions-to-lead-over-partition/59000 "2022-04-28T15:13:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [April 28, 2022, 3:13pm UTC](https://forum.inductiveautomation.com/t/sql-help-how-to-apply-conditions-to-lead-over-partition/59000/1 "2022-04-28T15:13:40Z")

</div>

For my query, I want to get the lead t\_stamp when the `table.code` column in that row is less than 1500.

```auto
select t_stamp, lead(t_stamp) over (order by t_stamp) as lead
from table

```

This gets me all t\_stamps and leads except the last lead. I was taught how to use coalesce to do that.

I tried the following. The t\_stamp gets filtered, but the lead ends up catching on codes higher than 1500.

```auto
select t_stamp, lead(t_stamp) over (order by t_stamp) as lead
from table
where code<1500

```

I tried a subquery, ~~but that didn’t work either. should it have worked?~~  
This works, but I think it will slow my query down.

```auto
select t_stamp, lead(t_stamp) over (order by t_stamp) as lead
from (
           select t_stamp from myTable
           where code < 1500
)subq
where code<1500

```
