Hi,
I'm after a bit of help/guidance please with a query I have with MS SQL Server (T-SQL)
I have a (large) table containing rows which can be grouped (PtName) and what I'd like to do is update each row and insert a field (Counter) that contains the record number of each field within it's group (i.e each record in its group will be numbered 1 to total number in group)..(Hopefully that's a suitable explanation!)
(This is so that later in the processes [Name] can be updated to be A1_001,A1_002,A1_026,B8_001 etc)
i.e
[pre]Name PtName Counter
A1a A1
A1b A1
A1c A1
... A1
A1z A1
B2a B2
B2b B2
D3a D3
D3b D3
D8a D8
[/pre]
would after update have
[pre]Name PtName Counter
A1a A1 1
A1b A1 2
A1c A1 3
... A1 ...
A1z A1 26
B2a B2 1
B2b B2 2
D3a D3 1
D3b D3 2
D8a D8 1
etc
[/pre]
Can this be done either via a query(s)/stored procedure..?
I can see that the Row_Number() OVER does something similar to what I want but I'd like it to be "reset" as each group is processed..
*EDIT Ok I've looked further into ROW_NUMBER and by using "ROW_Number() OVER (PARTITION by PtName Order By Name)" it gives me the expected results, but (still) interested in possible alternatives/confirmation if this is the correct way to go please if that's ok...
I've got it working in Access vba using various loops but having converted the back end to SQL and whilst the queries still work on the linked tables I've now been asked to utilise pure SQL functions (called initially via Pass-thru queries) as the Access FE is due to be replaced by a web app at some stage and they then just want to plug in the SQl functions that will already be in place....
Any questions please ask and any help/pointers gratefully accepted..
TIA
PaulSc
I'm after a bit of help/guidance please with a query I have with MS SQL Server (T-SQL)
I have a (large) table containing rows which can be grouped (PtName) and what I'd like to do is update each row and insert a field (Counter) that contains the record number of each field within it's group (i.e each record in its group will be numbered 1 to total number in group)..(Hopefully that's a suitable explanation!)
(This is so that later in the processes [Name] can be updated to be A1_001,A1_002,A1_026,B8_001 etc)
i.e
[pre]Name PtName Counter
A1a A1
A1b A1
A1c A1
... A1
A1z A1
B2a B2
B2b B2
D3a D3
D3b D3
D8a D8
[/pre]
would after update have
[pre]Name PtName Counter
A1a A1 1
A1b A1 2
A1c A1 3
... A1 ...
A1z A1 26
B2a B2 1
B2b B2 2
D3a D3 1
D3b D3 2
D8a D8 1
etc
[/pre]
Can this be done either via a query(s)/stored procedure..?
I can see that the Row_Number() OVER does something similar to what I want but I'd like it to be "reset" as each group is processed..
*EDIT Ok I've looked further into ROW_NUMBER and by using "ROW_Number() OVER (PARTITION by PtName Order By Name)" it gives me the expected results, but (still) interested in possible alternatives/confirmation if this is the correct way to go please if that's ok...
I've got it working in Access vba using various loops but having converted the back end to SQL and whilst the queries still work on the linked tables I've now been asked to utilise pure SQL functions (called initially via Pass-thru queries) as the Access FE is due to be replaced by a web app at some stage and they then just want to plug in the SQl functions that will already be in place....
Any questions please ask and any help/pointers gratefully accepted..
TIA
PaulSc