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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:11:38+00:00 2026-05-24T10:11:38+00:00

We have a sql database at work with a table, employees that has a

  • 0

We have a sql database at work with a table, employees that has a column, report_to, which contains the username of the person that that employee reports to. What we want to do is change this representation to a numerical representation. For instance:


‘a’ reports to ‘b’ reports to ‘c’. So the representation would be something like ‘a’ = 49, ‘b’ = 50, ‘c’ = 51. if ‘d’ becomes ‘c”s boss, then ‘d’ = 52. If ‘a’ becomes the supervisor of interns ‘e’ and ‘f’, then ‘e’ and ‘f’ both are equal to 48.


As shown, starting the numbers at a non zero number allows for expansion not only upwards but also down the hierarchical chain.

The main question is, how do I convert from the current structure (“report_to”), to a numerical representation?

NOTE: this is in MSSQL

  • 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-24T10:11:39+00:00Added an answer on May 24, 2026 at 10:11 am

    You can add a new column (rank) that should be 0.

    Then the first step is to find the BIG BOSS – this should be the user who doesn’t have a boss – report_to is null. His rank will be 1.

    The second step is to find his first directs. They will rank as 2. Something like:

    UPDATE TABLE SET RANK = 2 
    WHERE report_to IN 
     (SELECT username FROM TABLE WHERE RANK = 1)
    

    The third step is to find directs’s directs. Something like:

    UPDATE TABLE SET RANK = 3 
    WHERE report_to IN 
     (SELECT username FROM TABLE WHERE RANK = 2)
    

    The next steps are identical with step 2 and 3, until no RANK = 0 is found.

    All these steps can be done in a procedure, within a WHILE statement.

    In the end, if you would like to start the ranking from 50 instead on 1, then you can make an update:

    UPDATE TABLE SET RANK = 50 - RANK
    

    or to be sure you don’t miss anything:

    UPDATE TABLE SET RANK = (SELECT MAX(RANK) FROM TABLE) + 1 - RANK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL database that has a table with a field set to
I have a SQL database that has the following table: Table: PhoneRecords -------------- ID(identity
Learning php! I have a sql database which contains a table called 'images' which
I have a column in the database (SQL Server 2005) that has data with
I have a Sql Database (which I have no control over the schema) that
I have a database that logs when an employee has attended a course and
I have a SQL Server 2008 database table that uses uniqueidentifier as a primary
Here is what i have: a SQL CE database, that holds this Category table,
I have SQL Server 2008 database with 2 tables: Table A has columns ID
I have a Table that is generated using a SQL Database And put into

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.