标签:前一行 存在 tor mat 添加 记录 orm Inventor for
方案3的思路比较值得学习,我们将层层剖析它的结构:
1:对每一条记录找它的前一行
所谓连续的序列号就是看相同格式的情况下前一行记录与当前行的记录序号的值是否相差为1:
select numFormat,
lag(code, 1) over(partition by numFormat order by code) previousCode, code,
max(code) over(partition by numFormat) maxn
from inventory;
2:过滤掉中间连续序列号。
这里通过比较当前序列号与前一行的序列号相减是非相差为1,相差为1将被过滤掉;相差不为1比如2或者说有null值则不被过滤。
select * from(
select numFormat,
lag(code, 1) over(partition by numFormat order by code) previousCode, code,
max(code) over(partition by numFormat) maxn
from inventory
)
where nvl(code-previousCode-1,1) <> 0
;
3:把当前行的code列作为 startNum,后一行的previouscode列作为endNum
select numFormat,code startNum, lead(previousCode) over(partition BY numFormat order by previousCode ) endNum from(
select numFormat,
lag(code, 1) over(partition by numFormat order by code) previousCode, code,
max(code) over(partition by numFormat) maxn
from inventory
)
where nvl(code-previousCode-1,1) <> 0
;
但是我们这里却得到了错误的结果。
这里面有两个问题:
4:修复第3步的问题。
select numFormat,code startNum, nvl(lead(previousCode) over(partition BY numFormat order by previousCode nulls first ),maxn) endNum from(
select numFormat,
lag(code, 1) over(partition by numFormat order by code) previousCode, code,
max(code) over(partition by numFormat) maxn
from inventory
)
where nvl(code-previousCode-1,1) <> 0
;
注意红色部分的修改,这样就可以得到如下正确的结果。
标签:前一行 存在 tor mat 添加 记录 orm Inventor for
原文地址:https://www.cnblogs.com/pmh905001/p/12244514.html