我有像
这样的表格create table test(employee integer NOT NULL, code character varying(200), number integer)
我想自动增加列'数字'在每个插入记录
insert into test(employee, code) values(17,'bangalore')
insert into test(employee, code) values(17,'bangalore')
insert into test(employee, code) values(17,'mumbai')
我想要像
这样的结果employee code number
17 bangalore 1
17 bangalore 2
17 bangalore 3
17 mumbai 1
17 mumbai 2
17 bangalore 4
17 mumbai 3
18 bangalore 1
18 bangalore 2
18 mumbai 1
18 mumbai 2
答案 0 :(得分:1)
对于批量上传数据,请尝试以下方法是否有用。
创建临时表test2
create table test2(employee integer NOT NULL, code character varying(200))
insert into test2(employee, code) values(17,'bangalore')
insert into test2(employee, code) values(17,'bangalore')
insert into test2(employee, code) values(17,'mumbai')
与增量编号
一起插入实际表格insert into test(employee, code, number)
select employee, code, row_number() over (partition by code ) from test2
您可以包含 order by 子句,例如主键列或其他列,例如created_date:
over (partition by code order by created_date)
答案 1 :(得分:0)
$_REQUEST