Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于分区按Oracle添加列_Sql_Oracle - Fatal编程技术网

Sql 基于分区按Oracle添加列

Sql 基于分区按Oracle添加列,sql,oracle,Sql,Oracle,表: 目标是基于以下内容将新列Col4创建为1或0: 在带有Col2和Col3的每个分区中, 或者 如果Col1-1不存在或 如果表中存在Col1-1和Col1-2两个值, 那么Col4是1,否则是0。 输出表: Col1 Col2 Col3 43 1234 abc 42 1234 abc 41 1234 abc 35 1234 abc 34

表:

目标是基于以下内容将新列Col4创建为1或0:

在带有Col2和Col3的每个分区中, 或者

如果Col1-1不存在或 如果表中存在Col1-1和Col1-2两个值, 那么Col4是1,否则是0。 输出表:

Col1       Col2         Col3
43         1234         abc
42         1234         abc
41         1234         abc
35         1234         abc
34         5678         def

如果我遵循您描述的逻辑:

 Col1            Col2         Col3        Col4
 43              1234         abc         1
 42              1234         abc         0
 41              1234         abc         1
 35              1234         abc         1
 34              5678         def         1

是一个Dfiddle。

Col1-1是否表示Col1中的第一个值?我不确定您使用的是什么划分子句,或者不存在是什么意思。
select t.*,
       (case when lag(col1) over (partition by col2, col3 order by col1) <> col1 - 1
             then 1
             when lag(col1) over (partition by col2, col3 order by col1) is null
             then 1
             when lag(col1, 2) over (partition by col2, col3 order by col1) = col1 - 2
             then 1
             else 0
        end) as col4
from t;