Column1 Column2 Column3
Row1 x1 x2 y2
Row2 y1 y2 z2
Row3 z1 z2 (next row4 column2.value)
我正在尝试获取一个sql查询,其中我要从row2.column2打印column3
最佳答案
我认为您需要的是LAG
:
SQL> with test (id, col1, col2, col3) as
2 (select 1, 'x1', 'x2', null from dual union all
3 select 2, 'y1', 'y2', null from dual union all
4 select 3, 'z1', 'z2', null from dual
5 )
6 select id, col1, col2, lead(col2) over (order by id) col3
7 from test
8 order by id;
ID COL1 COL2 COL3
---------- ---- ---- ----
1 x1 x2 y2
2 y1 y2 z2
3 z1 z2
SQL>
我已经包含了 ID
列,因为您必须有一些东西来对行进行排序。不一定是ID
,可以是某个日期值或其他任何值。
关于sql - 是否有一个函数可以将sql中的下一行列值交换到当前行列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56475097/