Hi,
The sql below creates a temporary table that populates numeric ranges, basically I can define a numeric range i.e. 0 to 200 and the number of splits i.e. 4, the query then splits the range into 4 intervals.
The split column is my split id, so I know which is the 1st, 2nd, 3rd interval and so on.
The problem is the identity column is populating in reverse so the first interval 0 to 50 is Split = 4 (would like it to be split = 1)
The question is how can I modify the sql to reverse ths split column or have the identity populate in reverse.
Thank you
The sql below creates a temporary table that populates numeric ranges, basically I can define a numeric range i.e. 0 to 200 and the number of splits i.e. 4, the query then splits the range into 4 intervals.
The split column is my split id, so I know which is the 1st, 2nd, 3rd interval and so on.
The problem is the identity column is populating in reverse so the first interval 0 to 50 is Split = 4 (would like it to be split = 1)
The question is how can I modify the sql to reverse ths split column or have the identity populate in reverse.
Thank you
Code:
DECLARE @splitinterval TABLE (
[split] [int] identity(1, 1) NOT NULL
,[start] FLOAT
,[end] FLOAT
)
DECLARE @start FLOAT
,@end FLOAT
,@split INT
,@interval FLOAT
SET @start = 0
SET @end = 200
SET @split = 4
SET @interval = @end / @split
DECLARE @inc INT
SET @inc = 1
INSERT INTO @splitinterval
SELECT @interval * (@split - @inc)
,@interval * @split
WHILE (@inc < @split)
BEGIN
BEGIN
INSERT INTO @splitinterval
SELECT (@interval * (@split - @inc)) - @interval
,@interval * (@split - @inc)
SET @inc = @inc + 1
END
IF (@inc > @split)
BREAK
ELSE
CONTINUE
END
SELECT *
FROM @splitinterval
ORDER BY split asc