2

I have table like

create table test(employee integer NOT NULL, code character varying(200), number integer)

I want to auto increment column 'number' on every insert record

insert into test(employee, code) values(17,'bangalore')
insert into test(employee, code) values(17,'bangalore')
insert into test(employee, code) values(17,'mumbai')

I want result like

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
4
  • Do not post code or additional information in comments. edit your question. Commented Jun 2, 2017 at 5:57
  • 2
    Why do you want to store that information? You can easily generate those numbers while retrieving the data. Commented Jun 2, 2017 at 5:59
  • i want to store as the same data is referenced at multiple places Commented Jun 2, 2017 at 6:00
  • Your table has no primary key yet. And, with your design, it naturally would be the whole table. Why not just add a single serial to have a smaller primary key? It would make the ordering clear too. Commented Jun 2, 2017 at 7:34

2 Answers 2

1

For a batch upload of data, try if below approach would be useful.

create a temporary table 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 actual table along with incremental number

insert into test(employee, code, number) 
select employee, code, row_number() over (partition by code )  from test2

You could include order by clause like primary key column or another column like created_date :

over (partition by code order by created_date)
Sign up to request clarification or add additional context in comments.

Comments

0
create table test (employee integer NOT NULL, code character varying(200), number integer)

    insert into test(employee, code, number ) values(17,'bangalore',(select coalesce(max(number) + 1,1)  from test where employee = 17 and code = 'bangalore'));
    insert into test(employee, code, number ) values(17,'bangalore',(select coalesce(max(number) + 1,1)  from test where employee = 17 and code = 'bangalore'));
    insert into test(employee, code, number ) values(17,'mumbai',(select coalesce(max(number) + 1,1)  from test where employee = 17 and code = 'mumbai'));

5 Comments

I would use max rather than count. What if some rows are deleted?
@LaurenzAlbe, Yes you are right. and we have to change count(*) + 1 to coalesce(max(number) + 1,1).
This works fine for single insert, but i wanted for batch update.
This works fine for batch update but the same time if i do batch update second time then the problem comes.
what is problem can you please explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.