不重复相同的数据

时间:2020-07-08 01:39:14

标签: sql tsql

如图所示,想法是不要在“主管”列中重复相同的值。

select e.Name as Employee, s.Name as Supervisor 
from Employee e, Employee s  
where e.Employee_Supervisor = 2 and s.id = 2

I have this

I need this

1 个答案:

答案 0 :(得分:1)

如评论中所述,这可能应该由表示层来完成,但是。 。

select 
    e.Name AS Employee, 
    case when row_number() over (partition by s.Name order by e.Name)=1 
        then s.Name else '' end AS Supervisor 
from Employee e, Employee s  
where e.Employee_Supervisor=2 AND s.id=2

SQL Fiddle