Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 generating duplicate rows

Author  Topic 

scelamko
Constraint Violating Yak Guru

309 Posts

Posted - 2007-02-23 : 11:11:53
guys

I have 2 columns id and iid, I am trying to write tsql which will populate in the follow way

ID IID
1 4
2 4
3 4
4 5
5 5
6 5
7 6
8 6
9 6

basically for every 3 rows starting from 1 IID should be duplicated and for next 3 rows it should be duplicated by increment of 1

update test
set iid = 4

DECLARE @CURRENTID INT,
@CURRENTIID INT
SELECT @CURRENTID = [ID], @CURRENTIID = [IID]
FROM [test]
WHILE @CURRENTID <= MAX([id])
BEGIN

IF (@CURRENTID)/3 = 0
THEN @CURRENTIID = @CURRENTIID+1
ELSE
@CURRENTIID
END

any suggestions/inputs would help

Thanks

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-02-23 : 11:51:23
select ID, (ID-1)/3 + 4
from yourtable


should do the trick ....

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -