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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:20:37+00:00 2026-05-14T21:20:37+00:00

I have a table T (structure below) which initially contains all-NULL values in an

  • 0

I have a table T (structure below) which initially contains all-NULL values in an integer order column:

col1  varchar(30),
col2  varchar(30),
order int NULL

I also have a way to order the “colN” columns, e.g.

SELECT * FROM T ORDER BY some_expression_involving_col1_and_col2

What’s the best way to assign – IN SQL – numeric order values 1-N to the order table, so that the order values match the order of rows returned by the above ORDER BY?

In other words, I would like a single query (Sybase SQL syntax so no Oracle’s rowcount) which assigns order values so that SELECT * FROM T ORDER BY order returns 100% same order of rows as the query above.

The query does NOT necessarily need to update the table T in place, I’m OK with creating a copy of the table T2 if that’ll make the query simpler.

NOTE1: A solution must be real query or a set of queries, not involving a loop or a cursor.

NOTE2: Assume that the data is uniquely orderable according to the order by above – no need to worry about situation when 2 rows can be assigned the same order at random.

NOTE3: I would prefer a generic solution, but if you wish a specific example of ordering expression, let’s say:

SELECT * FROM T 
ORDER BY CASE WHEN col1="" THEN "AAAAAA" ELSE col1 END, ISNULL(col2, "ZZZ")
  • 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-14T21:20:38+00:00Added an answer on May 14, 2026 at 9:20 pm

    I used as an example the roysched table in pubs2. I added a column id (int NULL) and used as an ordering logic the following:

    substring(title_id,3,4) + right(replicate('0',5) + convert(varchar,lorange),5)
    

    Then I created (and updated) a view. To avoid duplicating, I used strings

    Declare @strSql varchar(1000), @strOrder varchar(256)
    set @strOrder = "substring(title_id,3,4) + right(replicate('0',5) + convert(varchar,lorange),5)"
    
    set @strSql = "create view my_roysched (id,my_order)
    as 
    select id, (select count(*) from (select " + @strOrder + " as my_order from roysched) BETA 
                  where BETA.my_order <= ALPHA.my_order) as my_id 
          from (select id," + @strOrder + " as my_order from roysched ) as ALPHA 
    
    update my_roysched 
    set id = my_order
    
    drop view my_roysched"
    
    exec (@strSql)
    

    Now if you give

    select * from roysched
    order by substring(title_id,3,4) + right(replicate('0',5) + convert(varchar,lorange),5)
    

    you will see that id match the order of rows returned by the above order by

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two table masterTbl and detailTbl which structure is given below... --PRIMARY TABLE
I have a table structure having a EMP_DATE column as below ID EMP_DATE ----
I have a table with below structure. I am not having control over changing
I have a mysql database with a table structure like below : Table Name
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a table that contains some data given below. It uses a tree
I have below table structure in MS SQL AirQuoteID Name SalesValue 7 M 49.50
i'm using curl to retrieve a page which has a table structure as below.
I have the a html structure with table header on which I am trying
I have done fastload for table which has more than one column (I did

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.