Results 1 to 2 of 2
  1. #1
    masoud_sedighy is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Dec 2011
    Posts
    78

    Reverse query for Hierarchical Data

    I have seen one article
    That use below data from category table

    category_id | name | parent |
    +-------------+----------------------+--------+
    | 1 | ELECTRONICS | NULL |
    | 2 | TELEVISIONS | 1 |
    | 3 | TUBE | 2 |
    | 4 | LCD | 2 |
    | 5 | PLASMA | 2 |
    | 6 | PORTABLE ELECTRONIC | 1 |
    | 7 | MP3 PLAYERS | 6 |
    | 8 | FLASH | 7 |
    | 9 | CD PLAYERS | 6 |
    | 10 | 2 WAY RADIOS | 6 |

    In the below query and returns below data.

    SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4


    FROM category AS t1
    LEFT JOIN category AS t2 ON t2.parent = t1.category_id
    LEFT JOIN category AS t3 ON t3.parent = t2.category_id
    LEFT JOIN category AS t4 ON t4.parent = t3.category_id
    WHERE t1.name = 'ELECTRONICS';

    +-------------+----------------------+--------------+-------+
    | lev1 | lev2 | lev3 | lev4 |
    +-------------+----------------------+--------------+-------+
    | ELECTRONICS | TELEVISIONS | TUBE | NULL |
    | ELECTRONICS | TELEVISIONS | LCD | NULL |
    | ELECTRONICS | TELEVISIONS | PLASMA | NULL |
    | ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH |
    | ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL |
    | ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL |
    +-------------+----------------------+--------------+-------+
    6 rows in set (0.00 sec)
    Now my question is if I have a table or query that has below data
    +-------------+----------------------+--------------+-------+
    | lev1 | lev2 | lev3 | lev4 |
    +-------------+----------------------+--------------+-------+
    | ELECTRONICS | TELEVISIONS | TUBE | NULL |
    | ELECTRONICS | TELEVISIONS | LCD | NULL |
    | ELECTRONICS | TELEVISIONS | PLASMA | NULL |
    | ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH |
    | ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL |
    | ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL |
    +-------------+----------------------+--------------+-------+
    How I can make a Reverse query that returns below data:
    category_id | name | parent |
    +-------------+----------------------+--------+
    | 1 | ELECTRONICS | NULL |
    | 2 | TELEVISIONS | 1 |
    | 3 | TUBE | 2 |
    | 4 | LCD | 2 |
    | 5 | PLASMA | 2 |
    | 6 | PORTABLE ELECTRONICS | 1 |
    | 7 | MP3 PLAYERS | 6 |
    | 8 | FLASH | 7 |
    | 9 | CD PLAYERS | 6 |
    | 10 | 2 WAY RADIOS | 6 |

    If I could have also level field in this reverse query was very good.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    What determines that MP3 PLAYERS, CD PLAYERS, 2 WAY RADIOS have parent 6 and TUBE, LCD, PLASMA have parent 2? They are all in the same field.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 0
    Last Post: 02-15-2013, 07:55 AM
  2. Hierarchical queries!!!
    By stoly in forum Access
    Replies: 10
    Last Post: 12-01-2010, 06:36 PM
  3. Displaying Hierarchical data
    By Gerry in forum Programming
    Replies: 10
    Last Post: 04-17-2010, 06:50 PM
  4. Reverse an .mde
    By Marion in forum Access
    Replies: 2
    Last Post: 12-07-2009, 03:09 PM
  5. Hierarchical reports with ADO shape
    By schetlur in forum Reports
    Replies: 0
    Last Post: 07-30-2009, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums