Table DDL:
create table test
(
inId int primary key identity,
inFK int not null,
inSeq int not null,
dtDate datetime
)
Data Insert:
insert into test
select 1,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 1,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 1,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 2,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 2,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 3,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 4,1, getdate()
If we select on this table:
inId inFK inSeq dtDate
----------- ----------- -----------
------------------------------------------------------
1 1 1 2005-02-01 12:54:40.967
2 1 1 2005-02-01 12:54:41.967
3 1 1 2005-02-01 12:54:42.967
4 2 1 2005-02-01 12:54:43.967
5 2 1 2005-02-01 12:54:44.967
6 3 1 2005-02-01 12:54:45.983
7 4 1 2005-02-01 12:54:47.077
(7 row(s) affected)
Problem:
What I would like to do (using SQL and not a cursor) is to update the
value of inSeq to its ordinal position, this will be based on the # of
occurences of inFK. For Example, I would like to run a sql statement
that would transform the data to:
update test
set inSEQ = (some sql)
select * from test - This would then produce:
inId inFK inSeq dtDate
----------- ----------- -----------
------------------------------------------------------
1 1 1 2005-02-01 12:54:40.967
2 1 2 2005-02-01 12:54:41.967
3 1 3 2005-02-01 12:54:42.967
4 2 1 2005-02-01 12:54:43.967
5 2 2 2005-02-01 12:54:44.967
6 3 1 2005-02-01 12:54:45.983
7 4 1 2005-02-01 12:54:47.077
(7 row(s) affected)
Any help would be greatly appreciated.
TFD
create table test
(
inId int primary key identity,
inFK int not null,
inSeq int not null,
dtDate datetime
)
Data Insert:
insert into test
select 1,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 1,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 1,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 2,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 2,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 3,1, getdate()
WAITFOR DELAY '000:00:01'
insert into test
select 4,1, getdate()
If we select on this table:
inId inFK inSeq dtDate
----------- ----------- -----------
------------------------------------------------------
1 1 1 2005-02-01 12:54:40.967
2 1 1 2005-02-01 12:54:41.967
3 1 1 2005-02-01 12:54:42.967
4 2 1 2005-02-01 12:54:43.967
5 2 1 2005-02-01 12:54:44.967
6 3 1 2005-02-01 12:54:45.983
7 4 1 2005-02-01 12:54:47.077
(7 row(s) affected)
Problem:
What I would like to do (using SQL and not a cursor) is to update the
value of inSeq to its ordinal position, this will be based on the # of
occurences of inFK. For Example, I would like to run a sql statement
that would transform the data to:
update test
set inSEQ = (some sql)
select * from test - This would then produce:
inId inFK inSeq dtDate
----------- ----------- -----------
------------------------------------------------------
1 1 1 2005-02-01 12:54:40.967
2 1 2 2005-02-01 12:54:41.967
3 1 3 2005-02-01 12:54:42.967
4 2 1 2005-02-01 12:54:43.967
5 2 2 2005-02-01 12:54:44.967
6 3 1 2005-02-01 12:54:45.983
7 4 1 2005-02-01 12:54:47.077
(7 row(s) affected)
Any help would be greatly appreciated.
TFD
Comment