IDENTITY is doing exactly what it is supposed to do - providing a unique "identity" number for each record you insert. you cannot use IDENTITY to do what you want (...directly but see the "riskier" route discussed below).
how do you measure "first visit", "second visit" etc?
one possible way is to have a VisitDT datetime field ..."first" is then John's earliest VisitDT, and so on.
so to generate your visit_number 'on the fly':
Code:
SELECT MRN
, VisitDT
, ROW_NUMBER() OVER(PARTITION BY MRN ORDER BY VisitDT) AS Visit_Number
FROM dbo.Patients;
no need to store this since you can generate it whenever you want it.
...SS2005 or higher!!
another riskier way is to have an IDENTITY field and to assume that the lowest IDENTITY value is the first visit ...i say "riskier" because it doesn't handle the oooops cases: forgotten first visit entered after the second visit; re-INSERT of a DELETEd record etc.
simply change to: ....PARTITION BY MRN ORDER BY YourIdentityField....
izy