2016-11-20 53 views
1

選擇行我有查詢,如下所示:的Oracle SQL - 水平最高的

SELECT CONNECT_BY_ROOT(name), LEVEL 
    FROM Workers 
    CONNECT BY PRIOR boss=nick 
    START WITH function IN ('programmer', 'designer'); 

我得到:

BELLA 1 
BELLA 2 
BELLA 3 
MICKEY 1 
MICKEY 2 
BOB  1 
BOB  2 
DUDE 1 
DUDE 2 
DUDE 3 
SONIA 1 
SONIA 2 
SONIA 3 
KATE 1 
KATE 2 
KATE 3 
LUKE 1 
LUKE 2 
LUKE 3 
LUKE 4 

我想是獲得最高級別名稱。 我的意思是:

BELLA 3 
MICKEY 2 
BOB  2 
DUDE 3 
SONIA 3 
KATE 3 
LUKE 4 

我試圖做到這一點,如下所示:

SELECT CONNECT_BY_ROOT(name), MAX(LEVEL) 
    FROM Workers 
    CONNECT BY PRIOR boss=nick 
    START WITH function IN ('programmer', 'designer') 
    GROUP BY CONNECT_BY_ROOT(name); 

,但它不工作。我得到'00979。 00000 - 不是GROUP BY表達式的錯誤。爲什麼?我怎樣才能使它工作?

回答

2

CONNECT_BY_ISLEAF

SELECT CONNECT_BY_ROOT(name), LEVEL 
FROM Workers 
WHERE connect_by_isleaf = 1 
CONNECT BY PRIOR boss=nick 
START WITH function IN ('programmer', 'designer'); 
+0

是的,這一切的一切,這是一點,謝謝:) – michalsol

2

一種方法是使用一個CTE:

with t(name, lev) as (
     SELECT CONNECT_BY_ROOT(name), LEVEL 
     FROM Workers 
     CONNECT BY PRIOR boss=nick 
     START WITH function IN ('programmer', 'designer') 
    ) 
select name, max(lev) 
from t 
group by name;