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 8272233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:04:09+00:00 2026-06-08T07:04:09+00:00

I have got a table in SQL Server 2008 where I need alternating values

  • 0

I have got a table in SQL Server 2008 where I need alternating values for one column, say column alt. Duplicates in that column always need the same value, hence I was thinking about using the dense_rank function for this column alt via % 2.

But there are also zip codes in that table, that I need to order the data by before assigning the alternating values.

So basically after the alternating values based on column alt have been assigned when the data is then ordered by zip code the alternating values really need to be alternating (apart from the duplicates in the ‘alt’ table of course).

Currently I get a result where the alt values do get alternating values, but when ordering by zip codes, I have sequences of e.g. 0,0,0 via the dense_rank function that are the problem.

I tried using a temp table, but didn’t get the expected result with a

select * into #txy ordered by zip 

and then doing the desk_rank on that table because the order of a temp table isn’t guaranteed.

Any ideas are greatly appreciated!

Cheers,
Stevo

Edit:

Sample Code:

CREATE TABLE [xy.TestTable](
[BaseForAlternatingValue] [char](10),
[zip] [varchar](5)
) ON [PRIMARY]
GO


INSERT INTO [xy.TestTable]
       ([BaseForAlternatingValue]
       ,[zip])
 VALUES
       ('cccccccccc','99999'),
       ('bbbbbbbbbb','22222'),
       ('aaaaaaaaaa','12345'),
       ('dddddddddd','33333'),
       ('aaaaaaaaaa','12345'),
       ('bbbbbbbbbb','22222')
GO

select (DENSE_RANK() OVER (ORDER BY BaseForAlternatingValue)) % 2 as AlternatingValue
    , BaseForAlternatingValue
    , zip
    from [xy.TestTable]
    order by zip


Result:
AlternatingValue    BaseForAlternatingValue zip
1                      aaaaaaaaaa            12345
1                      aaaaaaaaaa            12345
0                      bbbbbbbbbb            22222
0                      bbbbbbbbbb            22222
0                      dddddddddd            33333
1                      cccccccccc            99999

The Problem now is that when ordered by zip code the following columns both contain the same value (0) as alternating value. When ordered by zip code the result should really have alternating values, but these alternating values should be based on the column BaseForAlternatingValue.

0                      bbbbbbbbbb            22222
0                      dddddddddd            33333

The expected outcome should be:

AlternatingValue    BaseForAlternatingValue zip
1                      aaaaaaaaaa            12345
1                      aaaaaaaaaa            12345
0                      bbbbbbbbbb            22222
0                      bbbbbbbbbb            22222
1                      dddddddddd            33333
0                      cccccccccc            99999

The last AlternatingValue of the last two result rows is different: the Alternating Value needs to alternate between different zip codes. Before it was 0 for the third last row and also 0 for the second last row.

As for Mikael’s question below, “And what if you have add row (‘cccccccccc’,’12345′). What would the expected output be then?”

The expected output would then be:

AlternatingValue    BaseForAlternatingValue zip
1                      aaaaaaaaaa            12345
1                      aaaaaaaaaa            12345
0                      cccccccccc            12345
1                      bbbbbbbbbb            22222
1                      bbbbbbbbbb            22222
0                      dddddddddd            33333
0                      cccccccccc            99999

So in summary: I need alternating values for the column BaseForAlternatingValue, but this alternating should be visible when ordering by zip code. (and duplicates in BaseForAlternatingValue need the same “alternating” value)

—————-

In the end I found a simpler and relatively nice solution:
1) using a temp table with an insert into and order by and using id values (id values will reflect the order by clause)
2) finding out the smallest id for a given BaseForAlternatingValue
3) finding out the count of distinct BaseForAlternatingValues with an id smaller than that

  • 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-08T07:04:11+00:00Added an answer on June 8, 2026 at 7:04 am

    Try using ROW_NUMBER as a direct replacement for DENSE_RANK. DENSE_RANK will give multiple rows the same value where they tie for a rank – ROW_NUMBER will not.

    DENSE_RANK reference
    ROW_NUMBER reference

    EDIT

    This is ugly but appears to produce the correct result.
    The first CTE determines the output order of the rows and calculates the “alternating value”.
    The second determines the first instance of each BaseForAlternatingValue in the output result set.
    The output query returns the rows in the right order with the first “alternating value” for each BaseForAlternatingValue

    ;WITH cte
    AS
    (
    SELECT BaseForAlternatingValue, zip, 
           ROW_NUMBER() OVER (ORDER BY zip,BaseForAlternatingValue)AS rn,
           DENSE_RANK() OVER (ORDER BY zip,BaseForAlternatingValue) % 2 AS av
    FROM [xy.TestTable]
    )
    ,rnCTE
    AS
    (
    SELECT *, 
           ROW_NUMBER() OVER (PARTITION BY BaseForAlternatingValue ORDER BY rn) AS rn2
    FROM cte
    )
    SELECT rn.av AS AlternatingValue, 
           r.BaseForAlternatingValue, r.zip
    FROM cte r
    JOIN rnCTE rn
    ON rn.BaseForAlternatingValue = r.BaseForAlternatingValue
    AND rn.rn2 =1
    ORDER BY zip, BaseForAlternatingValue
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

System is: SQL Server 2008-R2 I have a table that one of its column
I have got a table in an SQL Server 2008 R2 database. It contains
In a SQL Server 2008 database, I have a column with multiple values separated
I'm using SQL Server 2008. I've got a column NVARCHAR(MAX) in a table which
I have got hold of a sql server 2008 developer edition and this is
I'm using SQL Server 2008. I have a table with over 3 million records,
I have a following table in SQL Server 2008 database: CREATE TABLE [dbo].[Actions]( [ActionId]
i have 3 tables in sql-server 2008 table A , table B , Table
I have a single table in the Sql Server 2008 r2 DB. Every few
I've got a table that people have been inserting into getting the primary key

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.