查询选择记录并插入3个副本记录,其中序列号列的增量为每三个记录的最大+1

时间:2012-10-07 20:17:02

标签: sql db2

需要在3个新行中复制1行,并在每个新行中增加序列号+1

insert into t1 (column1,column2,column3,sequencecolumn)
(  **row1
select *from t1 (column1,'101',column3,sequencecolumn)
(select max (sequencecolumn)+1 where column1 ='abc')
where column1 = 'abc'
)  
(   ***row2
select *from t1 (column1,'102',column3,sequencecolumn)
(select max (sequencecolumn)+1 where column1 ='abc')
where column1 = 'abc'
)
(  ***row3
select *from t1 (column1,'103',column3,sequencecolumn)
(select max (sequencecolumn)+1 where column1 ='abc')
where column1 = 'abc'
)

1 个答案:

答案 0 :(得分:1)

我可能会尝试这样的事情(如果我有一个方便的DB2安装):

INSERT INTO t1 (
  column1,
  column2,
  column3,
  sequencecolumn
)
SELECT
  t.column1,
  v.column2,
  t.column3,
  MAX(t.sequencecolumn) + v.rn
FROM t1 t
CROSS JOIN (VALUES (1, '101'), (2, '102'), (3, '103')) v (rn, column2)
GROUP BY
  t.column1,
  t.column3,
  v.column2,
  v.rn
WHERE t.column1 = 'abc'
;