hive 如果按列A划分的行号计数>1,则为特定列B填充空值

7gs2gvoe  于 6个月前  发布在  Hive
关注(0)|答案(2)|浏览(66)

表部门包含部门id和名称。当同一部门id不止一次时,需要在名称中填充为null。

Dept    Name  
200    ABC  
200    XYZ  
100    DEF

字符串
输出

Dept   Name  
200    NULL  
100    DEF

axr492tv

axr492tv1#

下面是使用窗口函数的解决方案。

select 
    distinct dept ,  
    case when count(*) over (partition by dept) > 1 then NULL 
         else [name] end as [Name]
from dept
order by dept;

字符串

cedebl8k

cedebl8k2#

执行GROUP BY。如果Dept只有一行,则返回其名称,否则返回NULL。

select Dept, case when count(*) = 1 then max(Name) end
from department
group by Dept

字符串

相关问题