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

  • Home
  • SEARCH
  • 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 8347683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:26:26+00:00 2026-06-09T07:26:26+00:00

Initially, I have two variables x (group variable) and y, now I’d like to

  • 0

Initially, I have two variables x (group variable) and y, now I’d like to create a third variable z which within each group of x starts from 1 and increase by 1 whenever y changes. Please see the following example. Could someone help how I can realize it in sql server 2008?

Thanks a lot!

x: a a a a a a a a b b b b b b b b
y: 0 0 0 1 0 0 2 3 0 1 0 0 2 3 0 0

Finally, I’d like to create z like this:

x: a a a a a a a a b b b b b b b b
y: 0 0 0 1 0 0 2 3 0 1 0 0 2 3 0 0
z: 1 1 1 2 3 3 4 5 1 2 3 3 4 5 6 6
  • 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-06-09T07:26:28+00:00Added an answer on June 9, 2026 at 7:26 am

    Here is the simple solution:

      declare @temp table ([row] int identity,[x] char,[y] int)
    insert into @temp([x],[y]) values ('a',0)
    insert into @temp([x],[y]) values ('a',0)
    insert into @temp([x],[y]) values ('a',0)
    insert into @temp([x],[y]) values ('a',1)
    insert into @temp([x],[y]) values ('a',0)
    insert into @temp([x],[y]) values ('a',0)
    insert into @temp([x],[y]) values ('a',2)
    insert into @temp([x],[y]) values ('a',3)
    insert into @temp([x],[y]) values ('b',0)
    insert into @temp([x],[y]) values ('b',1)
    insert into @temp([x],[y]) values ('b',0)
    insert into @temp([x],[y]) values ('b',0)
    insert into @temp([x],[y]) values ('b',2)
    insert into @temp([x],[y]) values ('b',3)
    insert into @temp([x],[y]) values ('b',0)
    insert into @temp([x],[y]) values ('b',0)
    
    --select * from @temp 
    
    DECLARE @int int=0
    ;WITH CTE as(
    select *,1 as incr from @temp where row=1 
    union all
    select t.*,
    CASE WHEN t.x=c.x then CASE WHEN t.y=c.y then incr else incr+1 end else CASE WHEN t.y=c.y then @int else @int+1 end end as incr
    from @temp t inner join CTE c
    on t.row=c.row+1
    )
    
    select * from CTE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain model that contains member variables for two languages, something like
I have a winforms app that initially displays a window with two file dialog
We have few components like libraries dlls When initially created I ran the following
I have a server, which uses a global variable to store the next available
I have two variables $a and $b and $a=Source/dir1/dir11 $b=Destination/dir1/dir11 $a and $b changes
I have two scenes. Scene A is the game scene, where the level variable
I have an iframe on a page that I need to initially have a
I have this jQuery datepicker code that initially set the minDate of the datepicker.
I have a page with a list of items initially loaded from a include_once('show_calendarV2.php');
I have a page where I am loading bunch of images initially when the

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.