Solved

WHILE LOOP TSQL

Posted on 2011-03-24
5
331 Views
Last Modified: 2012-05-11
Hi,
I would like some help with an sql script that is querying a temp table and populating another temp table.
My first table TEMPDATA has intervals IDFROM and IDTO, which are in metres.
I want to query the intervals by 10m but create a 1m interval and then a 9m interval, so:

TEMPDATA
ID    IDFROM   IDTO   LENGTH
A      0             5        5
A      5            10      15
A      10            20      10
A      20            35      15
A      35            65      30

TEMPDATA2 should be:
ID    IDFROM   IDTO   LENGTH
A      0             1        1
A      1             10        9
A      10              11        1
A      11              20        9
A      20              21        1
A      21              30        9
A      30              31        1
A      31              40        9
A      40              41        1
A      41              50        9
A      50              51        1
A      51              60        9
A      60              61        1
A      61              65        4


Thank you
DECLARE @TEMPDATA TABLE
	(ID VARCHAR(20)
	,IDFROM FLOAT
	,IDTO FLOAT
	,LENGTH FLOAT
	)

DECLARE @TEMPDATA2 TABLE
	(ID2 VARCHAR(20)
	,IDFROM2 FLOAT
	,IDTO2 FLOAT
	)

INSERT @TEMPDATA VALUES ('A','0','5','5')
INSERT @TEMPDATA VALUES ('A','5','10','15')
INSERT @TEMPDATA VALUES ('A','10','20','10')
INSERT @TEMPDATA VALUES ('A','20','35','15')
INSERT @TEMPDATA VALUES ('A','35','65','30')

DECLARE @ID VARCHAR(20)
DECLARE @IDFROM FLOAT
DECLARE @IDTO FLOAT
DECLARE @LENGTH FLOAT

DECLARE csr CURSOR FOR
SELECT * FROM @TEMPDATA

OPEN csr
FETCH NEXT FROM csr INTO @ID,@IDFROM,@IDTO,@LENGTH

WHILE @@FETCH_STATUS=0
BEGIN

WHILE @LENGTH >=10
				BEGIN
					IF(@LENGTH >= 10)
						BEGIN--B	
							SET @IDTO = @IDFROM + 10
									INSERT @TEMPDATA2 (ID2,IDFROM2,IDTO2)
									VALUES(@ID,@IDFROM,@IDTO-9)

									SET @LENGTH = @LENGTH - 10 - (@IDTO - CAST((@IDFROM) AS INT))
						END--B
				END--END WHILE LOOP

FETCH NEXT FROM csr INTO @ID,@IDFROM,@IDTO,@LENGTH

END
CLOSE csr
DEALLOCATE csr

SELECT * FROM @TEMPDATA2

Open in new window

0
Comment
Question by:crompnk
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:8080_Diver
Comment Utility
Try the attached:

DECLARE @TEMPDATA TABLE(ID   VARCHAR(20),IDFROM FLOAT,IDTO FLOAT,LENGTH FLOAT)
DECLARE @TEMPDATA2 TABLE(ID2 VARCHAR(20),IDFROM2 FLOAT,IDTO2 FLOAT)
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'0'
      ,'5'
      ,'5'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'5'
      ,'10'
      ,'15'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'10'
      ,'20'
      ,'10'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'20'
      ,'35'
      ,'15'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'35'
      ,'65'
      ,'30'
    )
DECLARE @ID VARCHAR(20)
DECLARE @IDFROM FLOAT
DECLARE @IDTO FLOAT 
DECLARE @LENGTH FLOAT
DECLARE @ModFlag INT;
DECLARE csr CURSOR FOR
SELECT  ID
       ,IDFROM
       ,(IDFROM % 10) ASW ModFlag
FROM @TEMPDATA
OPEN csr
FETCH NEXT
FROM csr
INTO @ID
  ,@IDFROM
  ,@ModFlag
--  ,@IDTO
--  ,@LENGTH
WHILE @@FETCH_STATUS = 0
BEGIN
	IF (@ModFlag = 0)
    BEGIN
	    SET @IDTO = @IDFROM + 1;
		SET @LENGTH = 1
--    WHILE @LENGTH >= 10
--    BEGIN
--        BEGIN--B

--     Insert the first row into @TempData2
            INSERT @TEMPDATA2
                (
                    ID2
                  ,IDFROM2
                  ,IDTO2
                )
                VALUES
                (
                    @ID
                  ,@IDFROM
                  ,@IDTO
                )


			SET	@IDFROM = @IDFROM + 1
			SET	@IDTO = @IDTO + 9
            SET @LENGTH = @LENGTH + 9

            INSERT @TEMPDATA2
                (
                    ID2
                  ,IDFROM2
                  ,IDTO2
                )
                VALUES
                (
                    @ID
                  ,@IDFROM
                  ,@IDTO
                )

        END--B
--    END    --END WHILE LOOP
    FETCH NEXT
    FROM csr
    INTO @ID
      ,@IDFROM
	  ,@ModFlag
--	  ,@IDTO
--	  ,@LENGTH
END
CLOSE csr
DEALLOCATE csr
SELECT *
FROM @TEMPDATA2

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
I prefer not to use loops. you can try CTE.
;with cte as (
select ID,MIN(IDFROM) IDFROM, MIN(IDFROM) + 10 NextID,MAX(IDTO) IDTO from @TEMPDATA group by ID union all
select ID,IDFROM + 10,case when NextID + 10 < IDTO then NextID+10 else IDTO end as NextID, IDTO from CTE where IDFROM + 10 <= IDTO)
select ID,IDFROM,IDTO from (
select 1 rn,ID,IDFROM,IDFROM + 1 as IDTO from cte union all
select 2,ID,NextID-1,NextID IDTO from cte) t1 order by IDFROM

Open in new window

Sample result with your data.
DECLARE @TEMPDATA TABLE
	(ID VARCHAR(20)
	,IDFROM FLOAT
	,IDTO FLOAT
	,LENGTH FLOAT
	)
INSERT @TEMPDATA VALUES ('A','0','5','5')
INSERT @TEMPDATA VALUES ('A','5','10','15')
INSERT @TEMPDATA VALUES ('A','10','20','10')
INSERT @TEMPDATA VALUES ('A','20','35','15')
INSERT @TEMPDATA VALUES ('A','35','65','30')

DECLARE @TEMPDATA2 TABLE
	(ID2 VARCHAR(20)
	,IDFROM2 FLOAT
	,IDTO2 FLOAT
	)

;with cte as (
select ID,MIN(IDFROM) IDFROM, MIN(IDFROM) + 10 NextID,MAX(IDTO) IDTO from @TEMPDATA group by ID union all
select ID,IDFROM + 10,case when NextID + 10 < IDTO then NextID+10 else IDTO end as NextID, IDTO from CTE where IDFROM + 10 <= IDTO)
INSERT @TEMPDATA2
select ID,IDFROM,IDTO from (
select 1 rn,ID,IDFROM,IDFROM + 1 as IDTO from cte union all
select 2,ID,NextID-1,NextID IDTO from cte) t1 order by IDFROM

SELECT * FROM @TEMPDATA2
/*
ID	IDFROM	IDTO
A	0	1
A	9	10
A	10	11
A	19	20
A	20	21
A	29	30
A	30	31
A	39	40
A	40	41
A	49	50
A	50	51
A	59	60
A	60	61
A	64	65
*/

Open in new window

Question - do you always have same ID. In this example A? If it is diffent ID, I may need to tweak the query little bit.
0
 

Author Comment

by:crompnk
Comment Utility
Hi, thanks for the help,

8080 Diver, I tried your script but it gave me the error:
Msg 402, Level 16, State 1, Line 47
The data types float and int are incompatible in the modulo operator.

If I use float values i.e. 10.2 I get the error, see code below.

Sharath 123, I've not used CTE before, bit tried the script, it was close but the output should be:
/*
ID      IDFROM      IDTO
A      0      1
A      9      10
A      10      11
A      11      20
A      20      21
A      21      30
A      30      31
A      31      40
A      40      41
A      41      50
A      50      51
A      51      60
A      60      61
A      61      65
*/

DECLARE @TEMPDATA TABLE(ID   VARCHAR(20),IDFROM FLOAT,IDTO FLOAT,LENGTH FLOAT)
DECLARE @TEMPDATA2 TABLE(ID2 VARCHAR(20),IDFROM2 FLOAT,IDTO2 FLOAT)
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'0'
      ,'5.2'
      ,'5.2'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'5.2'
      ,'10.2'
      ,'5'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'10.2'
      ,'22.2'
      ,'12.2'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'22.2'
      ,'35'
      ,'12.8'
    )
INSERT @TEMPDATA VALUES
    (
        'A'
      ,'35'
      ,'65'
      ,'30'
    )

--SELECT * FROM @TEMPDATA

DECLARE @ID VARCHAR(20)
DECLARE @IDFROM FLOAT
DECLARE @IDTO FLOAT 
DECLARE @LENGTH FLOAT
DECLARE @ModFlag INT
DECLARE csr CURSOR FOR
SELECT  ID
       ,IDFROM
       ,(IDFROM % 10) AS ModFlag
FROM @TEMPDATA
OPEN csr
FETCH NEXT
FROM csr
INTO @ID
  ,@IDFROM
  ,@ModFlag
WHILE @@FETCH_STATUS = 0
BEGIN
	IF (@ModFlag = 0)
    BEGIN
	    SET @IDTO = @IDFROM + 1
		SET @LENGTH = 1
--     Insert the first row into @TempData2
            INSERT @TEMPDATA2
                (
                    ID2
                  ,IDFROM2
                  ,IDTO2
                )
                VALUES
                (
                    @ID
                  ,@IDFROM
                  ,@IDTO
                )


			SET	@IDFROM = @IDFROM + 1
			SET	@IDTO = @IDTO + 9
            SET @LENGTH = @LENGTH + 9

            INSERT @TEMPDATA2
                (
                    ID2
                  ,IDFROM2
                  ,IDTO2
                )
                VALUES
                (
                    @ID
                  ,@IDFROM
                  ,@IDTO
                )

        END
    FETCH NEXT
    FROM csr
    INTO @ID
      ,@IDFROM
	  ,@ModFlag
END
CLOSE csr
DEALLOCATE csr
SELECT *
FROM @TEMPDATA2

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
Comment Utility
That would be because of the type that changed AS to ASW. :-/
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
I see the issue with my code. check this now.
DECLARE @TEMPDATA TABLE
	(ID VARCHAR(20)
	,IDFROM FLOAT
	,IDTO FLOAT
	,LENGTH FLOAT
	)
INSERT @TEMPDATA VALUES ('A','0','5','5')
INSERT @TEMPDATA VALUES ('A','5','10','15')
INSERT @TEMPDATA VALUES ('A','10','20','10')
INSERT @TEMPDATA VALUES ('A','20','35','15')
INSERT @TEMPDATA VALUES ('A','35','65','30')

DECLARE @TEMPDATA2 TABLE
	(ID2 VARCHAR(20)
	,IDFROM2 FLOAT
	,IDTO2 FLOAT
	)

;with cte as (
select ID,MIN(IDFROM) IDFROM, MIN(IDFROM) + 10 NextID,MAX(IDTO) MAX_IDTO from @TEMPDATA group by ID union all
select ID,IDFROM + 10,NextID+10, MAX_IDTO from CTE where IDFROM + 10 <= MAX_IDTO)
INSERT @TEMPDATA2
select ID,IDFROM,IDTO from (
select ID,IDFROM,case when IDTO < MAX_IDTO then IDTO else MAX_IDTO end IDTO,MAX_IDTO from (
select 1 rn,ID,IDFROM,IDFROM + 1 as IDTO,MAX_IDTO from cte union all
select 2,ID,NextID-9,NextID IDTO,MAX_IDTO from cte) t1 )t2
 where IDFROM < IDTO
 order by IDFROM

SELECT * FROM @TEMPDATA2
/*
ID2	IDFROM2	IDTO2
A	0	1
A	1	10
A	10	11
A	11	20
A	20	21
A	21	30
A	30	31
A	31	40
A	40	41
A	41	50
A	50	51
A	51	60
A	60	61
A	61	65
*/

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now