1

I have scenario, where I have to give sequence number for each record. And the sequence number should re-initialized to initial value if it is different account.

I was trying like this

declare @account_no varchar(50)
declare @seq_no int
declare @temp_no int
set @temp_no=1

select 
    (@account_no = (CASE WHEN @account_no <> PN.vst_int_id 
                           THEN PN.vst_int_id
                    END))
    ,(@seq_no = (CASE WHEN @account_no = PN.vst_int_id 
                        THEN @temp_no + 1
                      ELSE 1
                 END))
   ,@seq_no
   ,nte_pri_cd 
from 
   TSM310_PATIENT_NOTES PN
where 
   vst_int_id = '4588611'
0

1 Answer 1

6

Good news we have Ranking Functions in sql server to make these kind of operations very simple. Note it is only available in Sql Server 2005 and later editions.

select *,
       ROW_NUMBER() OVER (PARTITION BY AccountID ORDER BY SomeColumn) SequenceNumber

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

2 Comments

Thanks.. that's what I was looking for. I will get back to you if I will get any issue
Not a problem glad to help :)

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.