我有一个具体的问题.我有一个包含无效值的表.我需要用0大于的前一个值替换无效值(此处)0.
困难在于,对我来说使用Update或插入是不合适的(Cursor和update会这样做).我唯一的方法是使用Select语句.
当我lag(col1, 1)在大小写时使用- 函数时,我只得到一个具有正确值的列.
select col1, col2 realcol2,
(case
when col2 = 0 then
lag(col2,1,1) over (partition by col1 order by col1 )
else
col2
end ) col2,
col3 realcol3,
(case
when col3 = 0 then
lag(col3,1,1) over (partition by col1 order by col1 )
else
col3
end ) col3
from test_table
Run Code Online (Sandbox Code Playgroud)
内容TEST_TABLE:
---------------------------
Col1 | Col2 | Col3 | Col4
---------------------------
A | 0 | 1 | 5
B | …Run Code Online (Sandbox Code Playgroud)