views:

49

answers:

1
 1. Name---ID--- SEQ
 2. *---------101--1
 3. *---------101--2
 4. *---------101--3
 5. *---------999--1
 6. *---------999--2

Iqnore the -'s

What im importing is Name, ID, and other fields. What im trying to assign is SEQ. I'm not sure how to reset a count when I get to the 2nd group. Im using MS SQL Server 2005

+1  A: 

In your import you can calculate sequence as follows

INSERT INTO DestTable
SELECT Name, ID,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS SEQ
FROM SourceTable

From your example data it is unclear how you determine within an ID partition what is 1,2,3 etc. Maybe you would need to change the above to PARTITION BY ID ORDER BY Name or something else. At the moment it will be arbitrary.

Martin Smith
SEQ is just incremented for example if another row of data is entered with and ID of 101 it would be 4, and if a row of data is entered with 102 it would be 1
Tony
Well the query in my answer should do that for a one time import but why are you storing this anyway? You can easily use `ROW_NUMBER() OVER (PARTITION BY ID ORDER BY identitycol)` in your select queries and you won't have the hassle of trying to keep this updated ongoing.
Martin Smith
Do you have to address the situations where a row is deleted? Do the SEQ values change? What if a row is updated where ID changes?
bobs
That is all handled on the frontin database app
Tony