0

I'm trying to do something like this

key | series
____________
  1 |   1
  1 |   2
  2 |   1
  3 |   1
  3 |   2
  3 |   3
  4 |   1
  4 |   2

How do I go about generating a series for each unique key?

1 Answer 1

1

Use a window function ROW_NUMBER():

SELECT key, 
       row_number() over (partition by key) series
FROM table1
;

Demo: http://www.sqlfiddle.com/#!15/a6301/4

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

1 Comment

So awesome. Simple and elegant. Worked perfectly for me.

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.