Home > OS >  Build Sequence in postgres sql depending on the session
Build Sequence in postgres sql depending on the session

Time:12-25

In postgres sql, at the time of insert data from select to another table , I have to build a sequence depending upon the session, for every session sequence should have start from 1 and increment by one. when new session inserted sequence should start from 1.

enter image description here

CodePudding user response:

Calculate sequence on insert:

insert into mytable (session, sequence, ...)
values (1, (select count(*)   1 from mytable where session = 1), ...)

See live demo.

  • Related