0
Index   Columns         Text          New Index
===========================================
1   ISOCountry  TH          1
-------------------------------------------
2   Country        Thailand         1
-------------------------------------------
3   Region         Asia         1
-------------------------------------------
4   Date           12/31/2014       1
-------------------------------------------
5   Holiday        New Years Eve    1
-------------------------------------------
7   ISOCountry     DE           2
-------------------------------------------
8   Country        Germany          2
-------------------------------------------
9   Region         EMEA             2
-------------------------------------------
10  Date           12/31/2014       2
-------------------------------------------
11  Holiday        New Years Eve    2
-------------------------------------------
13  ISOCountry  DK          3
-------------------------------------------
14  Country         Denmark         3
-------------------------------------------
15  Region          EMEA            3
-------------------------------------------
16  Date            12/31/2014  3
-------------------------------------------
17  Holiday         New Years Eve   3
-------------------------------------------

I have a column as above. I need a loop/cursor in SQL Server such that everytime the Column "Column" changes to ISOCountry, the New Index column has to be incremented by 1. Which means that for Rows 1 through 5 the New Index column has a value 1, for rows 7 through 11 the New Index value has to be 2 and so on. The point where the Columns changes to ISO Country the New Index Value has to change to 2 likewise for the rest of the columns. I will have about 10000 rows in all

Any help in this regard will be greatly appreciated. Thanks

4
  • What version of SQL Server are you using? Commented Mar 28, 2014 at 18:48
  • I am using SQL Server 2008 Commented Mar 28, 2014 at 18:56
  • Why on earth would you store data that way? That is an extremely opoor table design. Commented Mar 28, 2014 at 19:01
  • We are getting data from a file in this format... I will use this to convert the table to Transpose the records Commented Mar 28, 2014 at 19:20

2 Answers 2

1

This will set the newIndex based on the number of ISOCountry records prior to the current record:

UPDATE A
SET newIndex = B.newIndex
FROM [myTable] A
CROSS APPLY (
    SELECT COUNT(*) [newIndex]
    FROM [myTable] X
    WHERE   X.[Index] <= A.[Index]
        AND X.[Columns] = 'ISOCountry'
) B
Sign up to request clarification or add additional context in comments.

Comments

1

Why use a loop? Just set the newindex value to the count of isocountry records with a lower id. You can do this with a correlated subquery (as well as with other methods):

update table t
    set newindex = (select count(*)
                    from table t2
                    where t2.columns = 'IsoCountry' and
                          t2."index" <= t."index"
                   );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.