Share via

Running Counter

Hursh 191 Reputation points
2022-07-15T03:10:25.42+00:00

Using the CTE code below; how do I also display a running counter like the following with the rest of the data? So the first pair of 4 records will have 1, 2, 3, 4 and the next pair of 4 records will have 1, 2, 3, 4 and so on.
MyCounter ID CustNum CustAddr CustCity xxxxxxxxxxxxxxxx
1
2
3
4
1
2
3
4
1
2
3
4

;with cte as  
 (  
    select ID, CustNum, CustAddr, CustCity, CustState, CustZip, CustPhone,OrderNumber  
    from #test  
 )  
 SELECT ID, CustNum, CustAddr, CustCity, CustState, CustZip, CustPhone  
 , c AS OrderNumber into #test2 from cte  
 CROSS APPLY (SELECT value  FROM STRING_SPLIT(OrderNumber, '|')) AS t(c)  
 select *from #test2  
  
Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


Answer accepted by question author

Jingyang Li 5,901 Reputation points Volunteer Moderator
2022-07-15T15:18:42.853+00:00

Cannot post code on this thread.

,(row_number() Over(partition by ID order by newid())-1)%4 +1 grp

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Hursh 191 Reputation points
    2022-07-15T16:30:49.56+00:00

    Thanks, it worked.

    Was this answer helpful?

    0 comments No comments

  2. Hursh 191 Reputation points
    2022-07-15T03:12:23.59+00:00

    DDL

    create table #test  
     (  
     ID INT,  
     CustNum INT,  
     CustAddr VARCHAR(30),  
     CustCity VARCHAR(30),  
     CustState CHAR(2),  
     CustZip CHAR(5),  
     CustPhone VARCHAR(20),  
     OrderNumber VARCHAR(MAX)  
     )  
     insert into #test values  
     (1, 1234, '55 Sunset Blvd', 'Hollywood', 'CA', '90045' ,'213-555-1212', '11111|22222|33333|444444');  
          
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.