0

postgres newbie here.

I have almost sequential data in a table - e.g.column data

seq 1 2 4 5 7

I am trying to write a query that returns the missing sequential numbers - eg 3,6 in this case. Not having much joy. Any help appreciated.

1 Answer 1

2

You can use generate_series to construct the full sequence, then join back to your table to filter out the existing values:

SELECT seq FROM (
  SELECT generate_series(MIN(seq), MAX(seq)) FROM t
) s (seq)
LEFT JOIN t USING (seq)
WHERE t.seq IS NULL
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Nick! much more elegant than what I was constructing.

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.