SELECT *
  FROM (
        /* Base Views collapsed */
        SELECT DISTINCT vi.view_id, vi.view_name, TO_NUMBER(NULL) AS pkg_state, TO_NUMBER(NULL) AS pv_id, NULL AS pkg_name, NULL AS pkg_version, NULL AS dlocked, NULL AS pv_description
          FROM release_content rel,
               view_settings vs,
               views vi
         WHERE rel.base_view_id = vi.view_id
           AND vs.view_id = rel.base_view_id
           AND vs.user_id = :USER_ID
           AND rtag_id = :RTAG_ID
           AND rel.base_view_id NOT IN ( /*SHOW_VIEWS*/ )
        UNION
        /* Base Views expanded */
        SELECT vi.view_id, vi.view_name, rel.pkg_state, pv.pv_id, pkg.pkg_name, pv.pkg_version, pv.dlocked, pv.pv_description
          FROM release_content rel,
               packages pkg,
               package_versions pv,
               views vi
         WHERE pv.pkg_id = pkg.pkg_id
           AND rel.pv_id = pv.pv_id
           AND rel.base_view_id = vi.view_id
           AND rel.base_view_id IN ( /*SHOW_VIEWS*/ )
           AND rtag_id = :RTAG_ID
         UNION
        /* Private Views collapsed */    
        SELECT vi.view_id, vi.view_name, TO_NUMBER(NULL) AS pkg_state, TO_NUMBER(NULL) AS pv_id, NULL AS pkg_name, NULL AS pkg_version, NULL AS dlocked, NULL AS pv_description
          FROM view_settings vs,
               view_def vd,
               views vi,
               release_content rel,
               package_versions pv
         WHERE vs.view_id = vi.view_id
           AND rel.pv_id = pv.pv_id
           AND vd.pkg_id = pv.pkg_id
           AND vd.view_id = vi.view_id
           AND vi.base_view = 'N'
           AND rel.rtag_id = :RTAG_ID
           AND vs.user_id = :USER_ID
           AND vi.view_id NOT IN ( /*SHOW_VIEWS*/ )
        UNION 
        /* Private Views expanded */
        SELECT vi.view_id, vi.view_name, rel.pkg_state, pv.pv_id, pkg.pkg_name, pv.pkg_version, pv.dlocked, pv.pv_description
          FROM users usr,
               view_settings vs,
               view_def vd,
               views vi,
               release_content rel,
               packages pkg,
               package_versions pv
         WHERE vs.user_id = usr.user_id
           AND vs.view_id = vi.view_id
           AND vd.view_id = vi.view_id
           AND pv.pkg_id = pkg.pkg_id
           AND rel.pv_id = pv.pv_id
           AND rel.rtag_id = :RTAG_ID
           AND vd.pkg_id = pkg.pkg_id
           AND vi.base_view = 'N'
           AND vi.view_id IN ( /*SHOW_VIEWS*/ )
           AND usr.user_id = :USER_ID
        ) ord
ORDER BY UPPER(ord.view_name), UPPER(ord.pkg_name) 