0

Looking for a solution In Postgresql

declare n_rows integer := select count(*) from sample;

SELECT generate_series AS serial_num
FROM generate_series(0, n_rows + 1)

1 Answer 1

1

SQL has no variables, so short of using a function, you can use a common table expression:

with var as (
   select count(*) as n_rows
   from sample
)
SELECT g.serial_num
FROM var, generate_series(0, n_rows + 1) as g(serial_num)

Or just:

SELECT *
FROM generate_series(0, (select count(*) + 1 from sample)) as g(serial_num) 
Sign up to request clarification or add additional context in comments.

Comments

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.