1
SELECT c.dept as department, c.name as manager, d.name as name 
FROM COMPLEX c  
JOIN COMPLEX d ON d.Manager = c.ID

使用上面的自加入查询我得到了这个输出

department  |   manager   | name
-------------------------------------
SW          |    Ranjan   | Deeksha

SW          |    Ranjan   | sham

SW          |    Ranjan   | Junaid

HQ          |    Vinay    | akhilesh

HQ          |    Vinay    | Pranav

但我需要最终输出为:

department  |   manager  | name
------------------------------------
SW          |    Ranjan  | Deeksha

            |            | sham

            |            | Junaid

HQ          |    Vinay   | akhilesh

            |            | Pranav
4

1 回答 1

0
SELECT 
NAME, CASE 
WHEN seq > 1 THEN ''
ELSE manager END AS Manager,
CASE 
WHEN seq > 1 THEN '' 
ELSE dept END AS dept 
FROM (SELECT b.NAME,b.dept,a.NAME AS manager, 
Row_number()OVER(partition BY a.dept ORDER BY a.dept DESC) seq 
FROM   complex a 
JOIN complex b ON a.id = b.manager) d     
于 2016-08-02T07:22:09.347 回答