部门表包含部门ID和部门名称。当同一部门 ID 多次出现时,需要将名称填充为 null。
Dept Name
200 ABC
200 XYZ
100 DEF
输出
Dept Name
200 NULL
100 DEF
做一个
GROUP BY
。如果部门只有一行,则返回其名称。否则返回 NULL。
select Dept, case when count(*) = 1 then max(Name) end
from tablename
group by Dept