CONNECT BY and START WITH clauses in oracle sql

user739115 picture user739115 · May 21, 2012 · Viewed 7.7k times · Source

I want hierarchical data for the test data given below. For user 'jones' it should fetch the below records. I am having problem with CONNECT BY and START WITH clauses. I also think there is some JOIN problem between menu_items AND role_menu_items tables.

for user and their corresponding roles i need to fecth below data

select  mi.id,  mi.NAME,mi.MEN_ID,mi.ICON,mi.ACTION--,r.NAME 
from
menu_items mi,
role_menu_items rmi,
roles r,
USERS U,
USER_ROLES UR
where
mi.ID=rmi.MIT_ID and
rmi.ROL_ID=r.id and
r.id=UR.ROLE_ID and
U.ID=UR.USER_ID and
U.NAME='jones'
start with mi.men_id is null
connect by   prior  mi.ID=mi.men_id

Output

1   Menu1           Menu1       /images/home.gif
3   Help            Help    
4   page1   a   1   page1   1   /images/email.gif
7   about   d   1   about   3

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (1,'Menu1',null,null,'Menu1',null,'/images/home.gif');

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (2,'Menu2',null,null,'Menu2',null,null);

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (3,'Help',null,null,'Help',null,null);

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (4,'page1','a',1,'page1',1,'/images/email.gif');

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (5,'page2','b',2,'page2',1,null);

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (6,'page3','c',1,'page3',2,'/images/photo.gif');

Insert into MENU_ITEMS (ID,NAME,SHORTCUT,DISPLAY_SEQUENCE,ACTION,MEN_ID,ICON) 
  values (7,'about','d',1,'about',3,null);

insert into ROLES (ID, NAME)
values (1, 'administrator');

insert into ROLES (ID, NAME)
values (2, 'user');

Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,4);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,5);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,6);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (1,7);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (2,4);
Insert into ROLE_MENU_ITEMS (ROL_ID,MIT_ID) values (2,7);

Insert into USER_ROLES (USER_ID,ROLE_ID) values (2,2);
Insert into USER_ROLES (USER_ID,ROLE_ID) values (1,1);
Insert into USER_ROLES (USER_ID,ROLE_ID) values (3,2);

Insert into USERS (ID,NAME,PASSWORD) values (2,'scott','scott');
Insert into USERS (ID,NAME,PASSWORD) values (1,'john','john');
Insert into USERS (ID,NAME,PASSWORD) values (3,'jones','jones');

Answer

turbanoff picture turbanoff · May 22, 2012

bad scheme, but it is possible

1. select all menu_id for user role


select RMI.MIT_ID
from ROLE_MENU_ITEMS RMI
   , ROLES R
   , USERS U
   , USER_ROLES UR
where RMI.ROL_ID = R.ID
  and R.ID = UR.ROLE_ID
  and U.ID = UR.USER_ID
  and U.NAME = 'jones'

2. reverse hierarhy query


select MI.*
     , max(LEVEL) over () + 1 - level as rev_level
     , lpad(' ', (max(LEVEL) over () + 1 - LEVEL) * 3, ' ') || MI.NAME as LEVEL_NAME
     , rownum RN
from MENU_ITEMS MI
connect by prior MEN_ID = ID
start with ID IN (select RMI.MIT_ID
                  from ROLE_MENU_ITEMS RMI
                     , ROLES R
                     , USERS U
                     , USER_ROLES UR
                  where RMI.ROL_ID = R.ID
                    and R.ID = UR.ROLE_ID
                    and U.ID = UR.USER_ID
                    and U.NAME = 'jones')

3. reverse (reverse hierarhy query)


select ID
     , NAME
     , SHORTCUT
     , DISPLAY_SEQUENCE
     , ACTION
     , MEN_ID
     , ICON
     , LEVEL_NAME
from 
(
    select MI.*
         , max(LEVEL) over () + 1 - level as rev_level
         , lpad(' ', (max(LEVEL) over () + 1 - LEVEL) * 3, ' ') || MI.NAME as LEVEL_NAME
         , rownum RN
    from MENU_ITEMS MI
    connect by prior MEN_ID = ID
    start with ID IN (select RMI.MIT_ID
                      from ROLE_MENU_ITEMS RMI
                         , ROLES R
                         , USERS U
                         , USER_ROLES UR
                      where RMI.ROL_ID = R.ID
                        and R.ID = UR.ROLE_ID
                        and U.ID = UR.USER_ID
                        and U.NAME = 'jones')
)
group by ID --distinct with order
       , name
       , SHORTCUT
       , DISPLAY_SEQUENCE
       , ACTION
       , MEN_ID
       , ICON
       , LEVEL_NAME
order by min(-RN)

possible to keep the sort of root items by ID, using a subquery

select ID
     , NAME
     , SHORTCUT
     , DISPLAY_SEQUENCE
     , ACTION
     , MEN_ID
     , ICON
     , LEVEL_NAME
from 
(
    select ID
         , NAME
         , SHORTCUT
         , DISPLAY_SEQUENCE
         , ACTION
         , MEN_ID
         , ICON
         , MAX(LVL) OVER () + 1 - LVL
         , rpad(' ', (max(LVL) over () + 1 - LVL) * 3, ' ') || NAME as LEVEL_NAME
         , RN
    from (
        select MI.*
             , rownum RN
             , level LVL
        from MENU_ITEMS MI
        connect by prior MEN_ID = ID
        start with ID IN (select RMI.MIT_ID
                          from ROLE_MENU_ITEMS RMI
                             , ROLES R
                             , USERS U
                             , USER_ROLES UR
                          where RMI.ROL_ID = R.ID
                            and R.ID = UR.ROLE_ID
                            and U.ID = UR.USER_ID
                            and U.NAME = 'jones')
        order siblings by -ID
    )
)
group by ID --distinct with order
       , name
       , SHORTCUT
       , DISPLAY_SEQUENCE
       , ACTION
       , MEN_ID
       , ICON
       , LEVEL_NAME
order by min(-RN)