Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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
动态更新序列值-plsql_Sql_Oracle_Sql Update - Fatal编程技术网

动态更新序列值-plsql

动态更新序列值-plsql,sql,oracle,sql-update,Sql,Oracle,Sql Update,我有一张表,有三列a列、b列和c列。列a和列b有值,列c有空值。我只想用a列对应b列的开始序列更新c列。col_c的期望值如下所示。使用游标实现此场景 COL_A COL_B COL_C 1 1 1 2 1 2 3 1 3 4 1 4 5 1

我有一张表,有三列a列、b列和c列。列a和列b有值,列c有空值。我只想用a列对应b列的开始序列更新c列。col_c的期望值如下所示。使用游标实现此场景

COL_A       COL_B       COL_C

     1          1         1   
     2          1         2  
     3          1         3 
     4          1         4  
     5          1         5  
     6          1         6  
     7          1         7  
     8          1         8  
     9          1         9  
    10          1        10  
   101          2       101
   102          2       102
   104          2       103
   106          2       104
   107          2       105
   108          2       106
   110          2       107
   201          3       201
   202          3       202
   203          3       203
   204          3       204
   205          3       205
   301          5       301
   302          5       302
   305          5       303
   306          5       304

一种方法是使用相关子查询,该子查询为每个colu_B记录集找到启动序列的最小colu_a值。对于该值,我们使用COUNT添加适当的偏移量


您可以利用以下分析功能:

Merge into your_table t
Using
(Select col_a, col_b,
       First_value(col_a) over (partition by col_b order by col_a)
       + row_number() over (partition by col_b order by col_a) - 1 as col_c
   From your_table) S
On (t.col_a = s.col_a and t.col_b = s.col_b)
When matched then 
Update set t.col_c = s.col_c

干杯

谢谢你的回复,先生。它正在反向更新。。。。SQL>更新tmp123 t1 SET COL_C=在t2.COL_A>t1.COL_A时选择MINt2.COL_A+COUNTCASE,然后从tmp123 t2中选择1,其中t2.COL_B=t1.COL_B;请重新加载你的页面,因为我已经更新了我的答案。这个更新查询非常有效。我应该在plsql块中使用游标吗?我不太熟悉Oracle中的游标。更新也可以独立运行。感谢您的帮助,Tim Biegeleisen先生了解了一个有用的场景。
Merge into your_table t
Using
(Select col_a, col_b,
       First_value(col_a) over (partition by col_b order by col_a)
       + row_number() over (partition by col_b order by col_a) - 1 as col_c
   From your_table) S
On (t.col_a = s.col_a and t.col_b = s.col_b)
When matched then 
Update set t.col_c = s.col_c