Running totals have been asked about a lot, I'll assume you're not just trying to sum the numbers. However, you can still probably use a self join and group by to get what you need, the final answer here just happened to be the first I saw that illustrated the technique. You do need to devise a formula that can express the previous number with this approach.
If the multiply by 8 is what you're actually trying to do, you could use ROW_NUMBER
and POWER
, assuming the default is 1:
INSERT INTO [my_table]([other_id], [number])
SELECT [id], POWER(8, ROW_NUMBER() OVER (ORDER BY [id]) - 1) AS [Num]
FROM [other_table]
If you had SQL Server 2012+, you could use the LAG
function:
INSERT INTO [my_table]([other_id], [number])
SELECT [id], LAG(Num, 1, 1) OVER (ORDER BY [id])
FROM [other_table]
EDIT:
Based on the comments, I think this should do the trick, as long as there aren't more than 12 things to schedule in an hour. And I'm sure there's a better way to convert that hour to a time
. I don't have my server handy to test.
INSERT INTO [my_table]([other_id], [other_time])
SELECT [id],
DATEADD(mi,
5 * (ROW_NUMBER() OVER (PARTITTION BY [hour_to_schedule] ORDER BY [id]) - 1),
CAST(CAST([hour_to_schedule] AS VARCHAR(2)) + ':00:00' AS TIME))
FROM [other_table]