[英]Lead and Lag Partitioning Oracle 12c
我有一個包含三列的表http://sqlfiddle.com/#!4/fbfb8/1
create table xx ( id number, stn number , str varchar2(20));
insert into xx (id,stn) values (1,2001);
insert into xx (id,stn) values (2,2002);
insert into xx (id,stn) values (3,2003);
insert into xx (id,stn) values (4,2004);
insert into xx (id,stn) values (1,3001);
insert into xx (id,stn) values (2,3002);
insert into xx (id,stn) values (3,3003);
對於第三列str的值應為
2001 -
2001 - 2002
2002 - 2003
2003 - 2004
3001 -
3001 - 3002
3002 - 3003
我的分區列應該是什么。 ?
謝謝
您的邏輯尚不清楚。 以下內容非常接近使用lag()
:
select xx.*,
(lag(stn) over (partition by trunc(stn / 1000) order by id) ||
' - ' || stn
) as str
from xx;
我不知道為什么您要在第一行中使用“ 2001-”而不是“-2001”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.