Reversing order of results in START WITH LPAD SQL

2019-09-14 03:40发布

问题:

SELECT LPAD('*', 2*level-1)||SYS_CONNECT_BY_PATH(unit_data, '/') "battle_unit_id"
   FROM battle_units
   where connect_by_isleaf = 1
   START WITH battle_unit_id= 600
   CONNECT BY PRIOR parent_id = battle_unit_id;

returns

*/F-16/Jet powered aircraft/Air/Doctrine

where, F-16 is the great-grandchild of Doctrine.

Question 1 I would like to reverse this order such that i get the following:

*/Doctrine/Air/Jet powered aircraft/F-16

Question 2 Currently, the battle_units table have battle_unit_id from 1 to 50,000 but the above query only gives me the hierarchy of ONE battle_unit_id. Is there way to get all hierarchies of 50,000 battle_unit_ids:

ie */Doctrine/Air/Jet powered aircraft/F-16 (id=30) */Doctrine/Air/Jet powered aircraft/F-22 (id=31) */Doctrine/Interstellar/Non-human/Galactic-Virus (id=45,321) ....etc

My oracle version:

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production

回答1:

Can you try this?

   SELECT LPAD('*', 2*level-1)||SYS_CONNECT_BY_PATH(unit_data, '/') "battle_unit_id"
   FROM battle_units
   START WITH parent_id IS NULL
   CONNECT BY PRIOR battle_unit_id = parent_id;

This should start with all battle-units that have no parents and get all children.