Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6610679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:52:17+00:00 2026-05-25T19:52:17+00:00

I have a SQL server 2008 table with the folling data in it: seq

  • 0

I have a SQL server 2008 table with the folling data in it:

seq | item
 1  |  A
 2  |  B
 3  |  C
 4  |  C
 5  |  C
 6  |  B
 7  |  D
 8  |  D
 9  |  C

I what return a new column which is a number which increments on change of item, as follows:

seq | item | Seq2
 1  |  A   |   1
 2  |  B   |   2
 3  |  C   |   3
 4  |  C   |   3
 5  |  C   |   3
 6  |  B   |   4
 7  |  D   |   5
 8  |  D   |   5
 9  |  C   |   6

The initial sequence must be maintained. Hope you can help, Tim

Edit:
I don’t what to update the table, just return the result set via a view or query. Thanks for all your efforts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:52:17+00:00Added an answer on May 25, 2026 at 7:52 pm
    declare @T table(seq int, item char(1))
    
    insert into @T values
    ( 1,    'A'),
    ( 2,    'B'),
    ( 3,    'C'),
    ( 4,    'C'),
    ( 5,    'C'),
    ( 6,    'B'),
    ( 7,    'D'),
    ( 8,    'D'),
    ( 9,    'C')
    
    ;with C as
    (
      select seq,
             item,
             1 as seq2
      from @T
      where seq = 1
      union all
      select T.seq,
             T.item,
             C.seq2 + case when C.item <> T.item then 1 else 0 end
      from @T as T
        inner join C
          on T.seq - 1 = C.seq
    )
    select seq,
           item,
           seq2
    from c       
    order by seq
    

    Update

    A version where seq is a datetime. I have added an extra CTE that enumerates the rows ordered by seq.

    declare @T table(seq datetime, item char(1))
    
    insert into @T values
    ( getdate()+1,    'A'),
    ( getdate()+2,    'B'),
    ( getdate()+3,    'C'),
    ( getdate()+4,    'C'),
    ( getdate()+5,    'C'),
    ( getdate()+6,    'B'),
    ( getdate()+7,    'D'),
    ( getdate()+8,    'D'),
    ( getdate()+9,    'C')
    
    ;with C1 as
    (
      select seq,
             item,
             row_number() over(order by seq) as rn
      from @T       
    ), 
    C2 as
    (
      select seq,
             item,
             rn,
             1 as seq2
      from C1
      where rn = 1
      union all
      select C1.seq,
             C1.item,
             C1.rn,
             C2.seq2 + case when C2.item <> C1.item then 1 else 0 end
      from C1
        inner join C2
          on C1.rn - 1 = C2.rn
    )
    select seq,
           item,
           seq2
    from C2       
    order by seq
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Money column in my SQL Server 2008 table. In my below
I have a table which contains my ads that can be searched in sql-server-2008.
We are using SQL Server 2008. We have a table called response which has
I have a SQL Server 2008 table with a DateTime column that I'd like
I have data in a SQL Server 2008 table that looks like this: I
I have a couple of images in my SQL Server 2008 table of which
Let's say I have some data, either in a SQL Server 2008 table or
I have a SQL Server 2008 table with a column of the geography datatype.
I have a SQL Server 2008 table which contains an external user reference currently
I have a SQL Server 2008 R2 table with nvarchar(4000) field. Data that stores

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.