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

The Archive Base Latest Questions

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

Table Schema: StatusId Status DisplayColor IsOpen DivisionId SortOrder 4 Open 3e696c 1 34 1

  • 0

Table Schema:

StatusId    Status  DisplayColor    IsOpen  DivisionId  SortOrder
4           Open    3e696c          1       34          1
5           Closed  3e696c          0       34          2
1           Open    3e696c          1       35          1
2           Closed  3e696c          0       35          2

The SQL Query I have to order the columns the way I need to number them:

select  StatusId, Status, DisplayColor, IsOpen, DivisionId, SortOrder
from    TV_Statuses s
order by DivisionId, IsOpen desc

I’m trying to create a script that can go through and assign SortOrder incrementally by DivisionId and then IsOpen. So here, STatusId of 4 would have a SortOrder of 1. StatusId of 5 would have SortOrder 2. The same for the bottom 2 rows, but here a division could have 100 rows, 70 open and 30 closed so the open would be numbered 1 – 70 and the Closed would be 71 – 100.

Any advice on a way to do this without resorting to cursors? My spidey sense tells me there’s probably a more straightforward way to do it.

Any assistance is appreciated.

EDIT:
OK, so actually in the database now, the SortOrder column only has NULLs in it. I want to update the SortOrder column so that each Division (using DivisionId) has an incrementing number in SortOrder. For instance above DivisioniId of 34 has to rows with one IsOPen = 1 and one IsOpen = 0. So I would want to order those two rows with SortOrder 1 and 2 respectively.
But a division could have 10 rows. Then that divisions SortOrder fields would be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
It’s hard to explain but hopefully that helps a little.

  • 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-18T07:26:13+00:00Added an answer on June 18, 2026 at 7:26 am

    Since SQL Server 2005 you can apply windowing functions like ROW_NUMBER() to a set (or a subset, often called a partition but not to be confused with table partitioning). I believe what you are looking for is the following:

    SELECT 
      StatusId, Status, DisplayColor, IsOpen, DivisionId, 
      SortOrder = ROW_NUMBER() OVER 
        (PARTITION BY DivisionId ORDER BY IsOpen DESC, StatusId)
    FROM dbo.TV_Statuses AS s
    ORDER BY DivisionId, IsOpen DESC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I am using asp.net 2.0 and have a different Sql Database table schema for
Say I have this table schema. ID AccNo Amount Say I have this data
I have a table schema which is essentially a bunch of transaction info with
edit: my table schema below is poorly normalized as suggested and I have revamped
Table Schema Table Name: file_manager_folder Rows: id , parentId , name My query simulates
I have a table schema similar to the following (simplified): CREATE TABLE Transactions (
Say you have a table schema such as this: friends(friendship_id, uid1, uid2, are_friends) .
Hey, I have a table with 2 fields of type int which are StatusID
I have followin table schema declare @temp table ( id int identity(1,1) not null,

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.