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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:59:38+00:00 2026-06-05T10:59:38+00:00

I’ve been given a SQL table that has a parent-child relationship that I’d like

  • 0

I’ve been given a SQL table that has a parent-child relationship that I’d like to present in a readable format.

The main columns involved are Drawer, Folder, Document. Each Drawer can have multiple folders associated to it, and each folder can have multiple documents associated to it. However, the Folder and Document are not listed in the same record.

This table contains item types, and a folder and document are both considered items. To associate them, the table assigns the itemnumber of the folder to the parentID in the document record.

For example:

DrawerID   ItemID    ParentID    Type    Name  
  1          1          0       Folder   Folder 1  
  1          2          0       Folder   Folder 2  
  1          3          0       Folder   Folder 3  
  1          4          0       Folder   Folder 4  
  1          5          1       Document Document A  
  1          6          1       Document Document B   
  1          7          1       Document Document C 
  1          8          2       Document Document A  
  1          9          3       Document Document A  
  1          10         3       Document Document B 

What I’m looking for is an output similar to this:

Drawer 1
    Folder 1
        Document A
        Document B
        Document C
    Folder 2
        Document A
    Folder 3
        Document A
        Document B    

The part that I’m stuck on is how to tie the ParentID to the ItemID. Would the best approach be some sort of union? recursive programming into a new table? I’m not a SQL person – I just cut and paste bits of queries, so please use little words 🙂

  • 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-05T10:59:40+00:00Added an answer on June 5, 2026 at 10:59 am

    This query should work, tested OK in SQL Server on your data. Basically it’s creating two pseudo tables in the query to represent the Folder and Document entities then joining together on the parent relationship:

    SELECT Folder.DrawerID, Folder.FolderId, Folder.FolderName, Doc.DocumentId, Doc.DocumentName
        FROM 
    
        (Select DrawerId, ItemId AS FolderId, ParentId, [Name] AS FolderName FROM Drawers WHERE [Type] ='Folder') Folder,
    
        (Select DrawerId, ItemId AS DocumentId, ParentId, [Name] AS DocumentName FROM Drawers  WHERE [Type] ='Document') Doc
    
        WHERE Doc.ParentId = Folder.FolderId
        ORDER BY DrawerId, FolderName, DocumentName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

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.