Monday, April 11, 2011

How to get lowest, common parent for 2 rows in recursive table (SQL)

Let's say that we have we have a table with the classic 'manager id' recursive relationship:

Users user_id int manager_id int (refers to user_id)

If you randomly select 2 rows in the table- or 2 nodes- how do you find the lowest level, common ancestor? My platform is SQL Server 2005 (Transact-SQL) but any ANSI compliant SQL will also work...

From stackoverflow
  • WITH
        hier1 (id, parent) AS (
        SELECT id, parent
        FROM table l
        WHERE id = @user1
        UNION ALL
        SELECT id, parent
        FROM table l, hier1 h
        WHERE l.id = parent
        ),
        hier2 (id, parent) AS (
        SELECT id, parent
        FROM table l
        WHERE id = @user2
        UNION ALL
        SELECT id, parent
        FROM table l, hier1 h
        WHERE l.id = parent
        ),
    SELECT  TOP 1 hier1.id
    FROM    hier1, hier2
    WHERE   hier1.id = hier2.id
    
  • A few minor edits to Quassnoi's answer, and it works:

    WITH
        hier1 (id, parent) AS (
        SELECT      id, parent
        FROM        table
        WHERE       id = @user1
        UNION ALL
        SELECT      id, parent
        FROM        table l, hier1 h
        WHERE       l.id = h.parent
        ),
        hier2 (id, parent) AS (
        SELECT      id, parent
        FROM        table
        WHERE       id = @user2
        UNION ALL
        SELECT      id, parent
        FROM        table l, hier1 h
        WHERE       l.id = h.parent
        )
    SELECT  TOP 1 hier1.id
    FROM    hier1, hier2
    WHERE   hier1.id = hier2.id
    
    Quassnoi : What were the edits? I don't see any difference :)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.