sql server - How to use LEAD and LAG In Where? -
i have table this:
number cost type date time ------ ----- ---- ---------- --------- 23456 0,665 sv 2014/02/02 08:00:02 23457 1,3 ec 2014/02/02 07:50:45 23460 0,668 sv 2014/02/02 07:36:34 23461 0,668 sv 2014/02/02 07:37:34 23462 0,668 sv 2014/02/02 07:38:34 23463 0,668 sv 2014/02/02 07:39:34
for each record need previous/next price. in case, query simple.
select lag(price) on (order date desc, time desc), lead(price) on (order date desc, time desc) items
but need result next cost <> record price
my query is
select lag(price) on (order date desc, time desc) nxt_price, lead(price) on (order date desc, time desc) prv_price items nxt_price <> cost
but refused utilize variable
try below query:
select nxt_price, prv_price (select price, lag(price) on (order date desc, time desc) nxt_price, lead(price) on (order date desc, time desc) prv_price items) innerquery nxt_price <> cost
it may help you.
sql-server lag lead
No comments:
Post a Comment