Reversing order of results in START WITH LPAD SQL query

依然范特西╮ 提交于 2019-12-25 01:53:06

问题


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.



来源:https://stackoverflow.com/questions/23111378/reversing-order-of-results-in-start-with-lpad-sql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!