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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:27:40+00:00 2026-05-26T04:27:40+00:00

Preface: I have looked all over stackoverflow.com and google for this. I have found

  • 0

Preface: I have looked all over stackoverflow.com and google for this. I have found hundreds of possible answers but either its not the correct SQL Server version or its not for SQL Server at all and I am not that adept to be able to adapt a query to TSQL for SQL Server 2000. Most of the examples assume that I would want to do some sort of aggregation – which I do not need to do. Also, many of the examples assume a fixed number of rows (an in yearly quarters transposing to 4 columns or at the lease a known number. I will have varying numbers of columns per row.

I have a table in SQL Server 2000 that stores words. It looks like this:

MAPENTRY_ID int NOT NULL
PARENT_ID int NOT NULL
ENCODED varchar(10) NOT NULL

MAPENTRY_ID is the primary key. PARENT_ID stores the MAPENTRY_ID of the “parent” word. Its a list of words and since these words are CaSe sensitive, there may be more than one “child” word for a given “parent”. If only one word/case exists (example below is “ABC” then the MAPENTRY_ID will be equal to the PARENT_ID.

Some example data looks like this:

MAPENTRY_ID PARENT_ID ENCODED
----------- --------- -------
8274302     8274302   abaco
8274306     8274302   Abaco
8274308     8274302   ABACO
5217985     5217985   abbynormal
5217987     5217985   Abbynormal
5217986     5217985   AbbyNormal
5217990     5217985   ABBYNORMAL
9285        9285      ABC
1144839     1144839   abc123
1144864     1144839   ABC123
5129019     5129019   abcapp
5129020     5129019   AbcApp
5129021     5129019   ABCAPP
8329759     8329759   abdominals
8329757     8329759   Abdominals
8329761     8329759   ABDOMINALS
8278875     8278875   abmill
8278878     8278875   abMill
8278879     8278875   abMILL
8278876     8278875   Abmill
8278877     8278875   AbMill
8278880     8278875   ABMILL
5217983     5217983   abnormal
5217993     5217983   Abnormal
5217994     5217983   ABNORMAL
8199838     8199838   aboutcopd
8199839     8199838   Aboutcopd
8199841     8199838   AboutCopd
8199840     8199838   AboutCOPD
8199845     8199838   ABOUTCOPD
8199733     8199733   aboutpad
8199756     8199733   Aboutpad
8199744     8199733   AboutPad
8199735     8199733   AboutPAD
8199765     8199733   ABOUTPAD
8199767     8199767   aboutrls
8199768     8199767   Aboutrls
8199770     8199767   AboutRls
8199772     8199767   AboutRLS
8199789     8199767   ABOUTRLS
8672422     8672422   abroad
8672423     8672422   Abroad
8672424     8672422   ABROAD
8478426     8478426   absecon
8478525     8478426   Absecon
8478582     8478426   ABSECON
8427765     8427765   absinthe
8427767     8427765   ABSINTHE
8690704     8690704   absolutely
8690705     8690704   Absolutely
8690706     8690704   ABSOLUTELY

After reading much material, I tried the following SQL:

SELECT MAPENTRY_ID, PARENT_ID, ENCODED
FROM XCO_MASTER
ORDER BY ENCODED, MAPENTRY_ID, PARENT_ID 

What it produced was logically correct but looks more like a tree structure. It really doesn’t look like more than a good “sort”… A small example is:

MAPENTRY_ID PARENT_ID ENCODED
----------- --------- -------
8274302     8274302   abaco
8274306     8274302   Abaco
8274308     8274302   ABACO
5217985     5217985   abbynormal
5217987     5217985   Abbynormal
5217986     5217985   AbbyNormal
5217990     5217985   ABBYNORMAL
9285        9285      ABC
1144839     1144839   abc123
1144864     1144839   ABC123

What I need to see is a result like:

MAPENTRY_ID ENCODED    ENCODED    ENCODED    ENCODED
----------- ---------- ---------- ---------- ----------
8274302     abaco      Abaco      ABACO      
5217985     abbynormal Abbynormal AbbyNormal ABBYNORMAL
9285        ABC
1144839     abc123     ABC123

And the reason that I would like it in that order is because I will want to display this information as an HTML table on a web site. I will not be displaying the entire table, only results from a keyword query that the user will be able to search for.

Is there any way to do this in SQL Server 2000 using TSQL? I would rather not parse the results of my query, above, on the client, in order to produce the desired results if it can be done with a specific type of query.

  • 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-26T04:27:40+00:00Added an answer on May 26, 2026 at 4:27 am

    Like some comments pointed I think doing this outside would be better.

    Nevertheless in TSQL you could do something like this:

    DECLARE @i int
    SET @i = 1
    DECLARE @sql_alter nvarchar(4000)
          , @sql_update nvarchar(4000)
          , @sql_select nvarchar(4000)
    
    CREATE TABLE #FLAT_TABLE (FID_0 int, FENCODED_0 varchar(10))
    SET @sql_select = N'SELECT FID_0, FENCODED_0'
    
    INSERT INTO #FLAT_TABLE (FID_0, FENCODED_0)
    SELECT MAPENTRY_ID
         , ENCODED
      FROM XCO_MASTER
     WHERE MAPENTRY_ID = PARENT_ID
    
    while (@@ROWCOUNT > 0) begin
    
        SET @sql_select = @sql_select + ', FID_' + @i
    
        SET @sql_alter = N'
            ALTER TABLE #FLAT_TABLE ADD COLUMN FID_' + @i + N' int
            ALTER TABLE #FLAT_TABLE ADD COLUMN FENCODED_' + @i + N' varchar(10)
        '
    
        SET @sql_update = N'
            UPDATE #FLAT_TABLE
               SET FID_' + @i + N' = MAPENTRY_ID
                 , FENCODED_' + @i + N' = ENCODED
              FROM XCO_MASTER
             WHERE MAPENTRY_ID <> PARENT_ID
               and MAPENTRY_ID = FID_' + (Cast (@i - 1) as nvarchar(8))
        '
        SET @i = @i + 1
    
        sp_executesql @sql_alter
        sp_executesql @sql_update
    end
    
    @sql_select = @sql_select + ' FROM #FLAT_TABLE'
    SELECT @sql_select
    

    This is just an idea, you will need to make some corrections (for example cast @i).

    Warning

    Be careful with the @@ROWCOUNT > 0 condition, it could lead to an infinite loop if you make a mistake.
    You can put something like @@ROWCOUNT > 0 and @i < @MAX_COLUMNS to avoid any problem.

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

Sidebar

Related Questions

Preface: I am sure this is incredibly simple, but I have searched this site
Let me preface this by saying I am extremely new to git but have
I'll preface this question with the note that I have looked at this similar
I have to preface this with the fact that I love jQuery as a
Preface: This is the first real swing program I have done. I have a
Let me preface by saying I have no Flash/AS knowledge really at all. My
All right, let me preface this by saying: I'm not completely confident this is
I'll preface this by saying I have minimal experience with both Perl and Socket
Let me preface this with.. I have extremely limited experience with ASM, and even
To preface, I'm very new to Obj-C but I do have some OOP experience

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.