简体   繁体   中英

SQL Query to extract nesting from a table

I have a table in which references are made from one row to another. So basically something like this:

ID | Name | ParentId
1  | Root | 
2  | Top  |  
3  | Sub1 | 1
4  | Sub2 | 2

The third column [ParentId] denotes the parent of the current row. My question is how do I get this nesting from the table using an SQL query. The result I would like to see from the query is

ID | Name | ParentName
1  | Root | NULL
2  | Top  | NULL
3  | Sub1 | Root
4  | Sub2 | Top

It might seem trivial as it is just replacing the id of the third column by the name of the item but I can't seem to figure it out. Thanks for you help!

for a single level you could use a left join on the same table

select a.id, a.name, b.name 
from my_table a  
left join my_table b on b.parentId = a.id 
select t1.id, t1.name, t2.name as parentName
from  table t1
left join  table t2
on t2.id = t1.parentId

The following query will help you to achieve your aim:

SELECT
    t1.Id,
    t1.Name,
    (SELECT t2.Name FROM MyTable t2 WHERE t2.Id = t1.ParentId) ParentName 
FROM
    MyTable t1

If there is a lot of records in the table, by creating an index on the ParentId column you can run the above query faster than the JOIN .

SQL Fiddle: hierarchyid example fiddles

I decided to take on a slightly different approach to answering this question, using the hierarchyid datatype that is supported natively inside of SQL server. hierarchyid was created to specifically handle record selection/query capabilities for nested relationships, and once you get used to some of the minor annoyances that come with the datatype (such as inline queries/subqueries which are often needed to arrive at the actual id value for a record), its actually pretty slick.

I created a SQL fiddle showing step by step how to use the data-type for an example nested relationship of:

/Category One
    /Subcategory 1
        /Subcategory 1.1
        /Subcategory 1.2
        /Subcategory 1.3
    /Subcategory 2
    /Subcategory 3
        /Subcategory 3.1
            /Subcategory 3.1.1
    /Subcategory 4
/Category Two
    /Subcategory 1

NB: the tsql is rewritten and annotated MSDN example code, which was actually quite poorly made imo. I have rewritten and presented it in such a way that I found it much easier to wrap my head around.

All proposed solution work fine for the posted question. In my "real" situation the table is far more complicated and has already many APPLY statements (specific SQL Server, I guess) so I ended up using the "Outer Apply" variant of posted Join statement: With a AS ( SELECT id, name from Nesting )
SELECT id, name, ParentName from a OUTER APPLY ( SELECT name as ParentName from Nesting n where n.parentid = a.id
) AS FOO
With a AS ( SELECT id, name from Nesting )
SELECT id, name, ParentName from a OUTER APPLY ( SELECT name as ParentName from Nesting n where n.parentid = a.id
) AS FOO

Thank you all so much!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM