简体   繁体   中英

SQL Lag function and where clause

I'm new in SQL and I want to write a query where we keep the rows where, for the same item:

  • column1_value is NULL AND
  • the previous not null column1_value is 'end' OR
  • the previous column1_value is NULL

Everything else is discarted.

I know I have to use LAG function i've not been able to reach to a final query. Can you help?

Thanks in advance

If I understand correctly:

select t.*
from (select t.*,
             lag(column1) over (order by ordercol) as prev_column1
      from t
     ) t
where column1 is null and 
      (prev_column1 = 'end' or prev_column1 is null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM