1
Id  Desc     Sequenceno
-----------------------------
84  TEST         1
84  TEST2        2
84  TEST3        3
85  IPhone       1
85  IPhone1      2
87  Nokia        1
88  Vivo         1
88  Vivo1        2

I have Id and Desc Column. How can I get Sequenceno Column like above?

Thanks in Advance.

3
  • What platform of DB are you using? Commented Jan 20, 2018 at 4:46
  • Thanx Gordon to edit my question Commented Jan 20, 2018 at 4:48
  • platform SQL Server Commented Jan 20, 2018 at 4:48

2 Answers 2

4

Use the ANSI-standard row_number() function:

select t.*, row_number() over (partition by id order by [desc]) as seqnum
from t;
Sign up to request clarification or add additional context in comments.

3 Comments

select t.*, row_number() over (partition by id order by id desc) as seqnum from t
Thanks for hint Gordon
@Sagarpadhiyar . . . desc is the name of the column. Very bad name because it conflicts with a keyword. I fixed the answer.
1

Try this:

SELECT
  Id, [Desc], 
  ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Id) AS Sequenceno
FROM #Temp;

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.