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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:04:22+00:00 2026-05-26T10:04:22+00:00

I have a table (EmpInfo) where employee info stored. Here are the fields: EMPLID

  • 0

I have a table (EmpInfo) where employee info stored. Here are the fields:

EMPLID – varchar(11) (PK)
NAME – varchar(50)
POSITION_NBR – varchar(8)
CREPORTS5 – char(8)
CREPORTS6 – char(8)
CREPORTS7 – char(8)

The CREPORTS field references the POSITION_NBR field in a chain of command.

I’m trying to write a SQL query where I can return the name of CREPORTS instead of the POSITION_NBR

Hierarchy Structure:

Jack Jones – CEO
  John Smith – Manager
      Sarah Test – Supervisor
         Suzy Blerg – Worker
         Mike Martin – Worker

-------------------------------------------------------------------------------- 
|  EMPLID  |     NAME      | POSITION_NBR  | CREPORTS5 | CREPORTS6 | CREPORTS7 | 
-------------------------------------------------------------------------------- 
|   1234   |  John Smith   |    11111      |   22222   |   NULL    |   NULL    |
-------------------------------------------------------------------------------- 
|   2345   |  Jack Jones   |    22222      |   NULL    |   NULL    |   NULL    |
--------------------------------------------------------------------------------  
|   3456   |  Sarah Test   |    33333      |   22222   |   11111   |   NULL    |
--------------------------------------------------------------------------------  
|   4567   |  Suzy Blerg   |    44444      |   22222   |   11111   |   33333   |
-------------------------------------------------------------------------------- 
|   5678   |  Mike Martin  |    55555      |   22222   |   11111   |   33333   |
--------------------------------------------------------------------------------

I’m not much of a SQL guy, so my first attempt went like this:

SELECT     EmpInfo.EMPLID    
               EmpInfo.CREPORTS5,
                  CAST
                    ((SELECT     TOP (1) NAME
                      FROM         EmpInfo AS subInfo
                      WHERE     (subInfo.POSITION_NBR = EmpInfo.CREPORTS5)) AS varchar(50)) AS CREPORTS5_NAME
FROM EmpInfo

This query works, but is slow when running it against 300k records.

  • 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-26T10:04:22+00:00Added an answer on May 26, 2026 at 10:04 am

    Not sure, but I think this is what you want; just do a self join to the table multiple times:

    DECLARE @t TABLE
        (
          EMPLID VARCHAR(11)
        , NAME VARCHAR(50)
        , POSITION_NBR VARCHAR(8)
        , CREPORTS5 CHAR(8)
        , CREPORTS6 CHAR(8)
        , CREPORTS7 CHAR(8)
        )
    
    
    INSERT  INTO @t
            ( EMPLID, NAME, POSITION_NBR, CREPORTS5, CREPORTS6, CREPORTS7 )
    VALUES  ( '1234', 'John Smith', '11111', '22222', NULL, NULL ),
            ( '2345', 'Jack Jones', '22222', NULL, NULL, NULL ),
            ( '3456', 'Sarah Test', '33333', '22222', '11111', NULL ) ,
            ( '4567', 'Suzy Blerg', '44444', '22222', '11111', '33333' ),
            ( '5678', 'Mike Martin', '55555', '22222', '11111', '33333' ) 
    
    
    SELECT  t.EMPLID
          , t.Name
          , t2.NAME AS CDR5Name
          , t3.NAME AS CDR6Name
          , t4.NAME AS CDR7Name
    FROM    @t t
            LEFT JOIN @t t2 ON t.CREPORTS5 = t2.POSITION_NBR
            LEFT JOIN @t t3 ON t.CREPORTS6 = t3.POSITION_NBR
            LEFT JOIN @t t4 ON t.CREPORTS7 = t4.POSITION_NBR
    

    Also, you may want to change the datatypes to match (use a numeric type for numerical data, etc). It would also be nice if you could post a sample script instead of a description 🙂

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

Sidebar

Related Questions

I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have the following table in a MySql database: **EmpInfo** ID Name Address --------------------------------------------------------
I have table definition like below: Place (id, name) Review (id, userid, placeid) Favorite
I have table with 50 entries (users with such details like Name Surname Location
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have table with 5 columns ------------------------------------------------------ |_id(mongo default)| link | name| street|zip |
I have table Players with columns: ID Name Points What is most efficient way
I have table with fields: email , password_hash and salt . On user registration,
I have table named x Here is the sample data in table x Column1_
I have table with data about tasks like Id, Name, Date, WorkerId, VehicleId (and

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.