I have a table that looks like this:
RANGE Directories
5136451 ABC
51364522 ABC
5136453 XYZ
5136454 ABC
5136455 XYZ
5136456 CDE
5136457 ABC
5136458 ABC
5136459 XYZ
51364511 CDE
51364512 ABC
51364513 ABC
51364514 ABC
51364515 ABC
51364516 ABC
The Result should be:
DIRECTORY BeginRange EndRange
ABC 5136451 5136451
ABC 51364522 51364522 (they aren't sequential so then get ind. lines
XYZ 5136453 5136453
ABC 5136454 5136454
XYZ 5136455 5136455
CDE 5136456 5136456
ABC 5136457 5136458 There are two ABC's in a row and so the begin is the first, and the end is the last instance before the next Directory. and they are sequential
XYZ
CDE
ABC 51364512 51364516 (this is how the last set of ABCs should look because they are the same directory and in sequential order, but they are coming out like this:
ABC 51364512 51364513
ABC 51364514 51364515
ABC 51364516 51364516
HERE IS MY CODE:
Can anyone tell me what I'm missing... THANKS
RANGE Directories
5136451 ABC
51364522 ABC
5136453 XYZ
5136454 ABC
5136455 XYZ
5136456 CDE
5136457 ABC
5136458 ABC
5136459 XYZ
51364511 CDE
51364512 ABC
51364513 ABC
51364514 ABC
51364515 ABC
51364516 ABC
The Result should be:
DIRECTORY BeginRange EndRange
ABC 5136451 5136451
ABC 51364522 51364522 (they aren't sequential so then get ind. lines
XYZ 5136453 5136453
ABC 5136454 5136454
XYZ 5136455 5136455
CDE 5136456 5136456
ABC 5136457 5136458 There are two ABC's in a row and so the begin is the first, and the end is the last instance before the next Directory. and they are sequential
XYZ
CDE
ABC 51364512 51364516 (this is how the last set of ABCs should look because they are the same directory and in sequential order, but they are coming out like this:
ABC 51364512 51364513
ABC 51364514 51364515
ABC 51364516 51364516
HERE IS MY CODE:
Code:
DROP TABLE #testcomp
CREATE TABLE #Testcomp(Directory varchar(30), BeginRange nvarchar(20), EndRange nvarchar(20))
DECLARE @Begin as nvarchar (20)
DECLARE @End as nvarchar(20)
DECLARE @comp as nvarchar(20)
DECLARE @Cdir as varchar (30)
DECLARE @Range as nvarchar (20)
DECLARE @Directory as varchar(30)
DECLARE Compression_Cursor
CURSOR FOR SELECT Range, Directories FROM Tbl_Test_Rtg
OPEN Compression_Cursor
FETCH NEXT FROM Compression_Cursor INTO @Range, @Directory
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Begin = @range
SET @CDir = @Directory
SET @End = @range
FETCH NEXT FROM Compression_Cursor INTO @Range, @Directory
SET @comp = @range
IF @Directory = @Cdir
BEGIN
IF @comp = @End + 1
BEGIN
SET @End = @comp
FETCH NEXT FROM Compression_Cursor INTO @Range, @Directory
END
ELSE
SET @End = @Begin
END
ELSE
SET @End = @Begin
INSERT INTO #Testcomp(Directory,BeginRange,EndRange)
VALUES(@Cdir, @Begin, @End)
END
CLOSE Compression_Cursor
DEALLOCATE Compression_Cursor
SELECT * FROM #testcomp
SELECT * FROM Tbl_Test_Rtg
Can anyone tell me what I'm missing... THANKS