0

I have to create table in postgres with serno,id,name,address and the serno should be a running number for each id which comes from other table. I have the folowing query

CREATE TABLE Ids (SerNo integer, Id varchar(100),Name varchar(250),Address varchar(500));

INSERT INTO Ids (SerNo,Id,Name,Address) 
VALUES (rank() OVER(ORDER BY "Id"),
(SELECT distinct("Id") from   "Table2"),'John','US');

ERROR:  window functions are not allowed in VALUES
LINE 2: VALUES (rank() OVER(ORDER BY "Id"),

Please correct me

1 Answer 1

1

Use a sequence:

create table Ids (SerNo serial...

insert into Ids (Id,Name,Address) 
select distinct Id, 'John', 'US'
from Table2;

https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

Sign up to request clarification or add additional context in comments.

1 Comment

Got it !! Thank you.

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.