2

Below is a table in databasse.

A is main item . aas,asd and ads is sub item of A

B is main item . byw and bmw is sub item of A

  ____Main Item_____|________Sub Item_______
          A         |          aas                             
          A         |          asd
          A         |          ads
          B         |          byw
          B         |          bmw

I need to display like these. It will added SEQ_NO column..

  ____Main Item_____|_____SEQ_NO___|____Sub Item_______
          A         |       1      |      aas                             
          A         |       2      |      asd
          A         |       3      |      ads
          B         |       1      |      byw
          B         |       2      |      bmw

I use sql server.

2
  • please tag your database engine Commented Sep 21, 2015 at 22:55
  • How is this ordered? How come asd comes after aas, but before ads? Commented Sep 21, 2015 at 22:57

1 Answer 1

3

You can use row_number function to generate the seq_no column. Also change the sub_item ordering as per your requirements.

select main_item,
row_number() over(partition by main_item order by sub_item) as seq_no,
sub_item 
from tablename
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.