日期:2014-05-17 浏览次数:21022 次
select decode(moduleid, rootmoduleid, sum) sum, moduleid, modulename, parentid, leaf, "level", orderid from (select sum(1) over(partition by rootmoduleid) sum, t1.* from (select t.*, connect_by_root moduleid rootmoduleid from t start with "level"=2 connect by prior moduleid=parentid) t1 order by rootmoduleid, parentid, orderid);
------解决方案--------------------
--moduleid modulename parentid leaf level orderid
WITH t AS
(SELECT 1 AS mId,
'菜单1' AS mName,
0 AS pid,
1 AS leaf,
2 AS lvl,
1 AS orderid
FROM dual
UNION ALL
SELECT 2, '菜单2', 0, 1, 2, 2 FROM dual
UNION ALL
SELECT 11, '菜单11', 0, 1, 2, 11 FROM dual
UNION ALL
SELECT 12, '菜单12', 1, 0, 3, 1 FROM dual
UNION ALL
SELECT 13, '菜单13', 1,1, 3, 1 FROM dual
UNION ALL
SELECT 14, '菜单14', 11, 1, 3, 1 FROM dual
UNION ALL
SELECT 215, '菜单215', 13, 1, 4, 1 FROM dual
UNION ALL
SELECT 216, '菜单216', 14, 0, 4, 1 FROM dual
UNION ALL
SELECT 217, '菜单217', 215, 0, 5, 1 FROM dual
)
SELECT mid,
mname,
pid,
leaf,
lvl,
orderid,
DECODE(lvl,2,aa,0) AS "SUM"
FROM
(SELECT mid,
mname,
pid,
leaf,
lvl,
orderid,
(SELECT COUNT(1)
FROM t t1
CONNECT BY prior t1.mid=t1.pid
START WITH t1.mid = a.mid
) AS aa
FROM
( SELECT * FROM t CONNECT BY prior mid = pid START WITH pid = 0
) a
)
MID MNAME PID LEAF LVL ORDERID SUM
---------------------- --------- ---------------------- ---------------------- ---------------------- ---------------------- ----------------------
1 菜单1 0 1 2 1 5
12 菜单12 1 0 3 1 0
13 菜单13 1 1 3 1 0
215