(1)当distinct应用到多个字段的时候,distinct必须放在开头,其应用的范围是其后面的所有字段,而不只是紧挨着它的一个字段,而且distinct只能放到所有字段的前面
(2)distinct对NULL是不进行过滤的,即返回的结果中是包含NULL值的
(3)聚合函数中的DISTINCT,如 COUNT( ) 会过滤掉为NULL 的项
注意:ROW_Number() over (partition by id order by time DESC) 给每个id加一列按时间倒叙的rank值,取rank=1
select m.id,m.gender,m.age,m.rank
from (select id,gender,age,ROW_Number() over(partition by id order by id) rank
from temp.control_201804to201806
where id!='NA' and gender!='' or age!=''
) m
where m.rank=1
%jdbc(hive)
create table temp.match_relation_3M_active_v5 as
select a.id
from (select distinct id,superid
from temp.match_relation_3M_activ
order by superid desc
limit 100
) a
group by a.id
注意,对id去重时可以用gruop by 或者distinct id,两者去重后的id排序时一致的,但是加了distinct(group by)后,distinct字段自带排序功能,会先按照distinct后面的字段进行排序,即已经改变了子查询的中order by的排序,但是结果与正确结果中的id是一样的,只是排序不同罢了。
%jdbc(hive)
create table temp.match_relation_3M_active_v7 as
select a.id
from (select id,max(superid) as superid
from temp.match_relation_3M_active
group by id
order by superid desc
limit 100
) a
%jdbc(hive)
create table temp.match_relation_3M_active_v11 as
select n.id
from (select m.id,superid
from (select id,superid,ROW_Number() over(partition by id order by id) rank
from temp.match_relation_3M_active
) m
where m.rank=1
order by superid desc
limit 100
)n
注意,以下代码中,窗口函数ROW_Number() over()的执行顺序晚于 order by superid desc,最终的结果并非 superid的倒叙排列的结果
%jdbc(hive)
create table temp.match_relation_3M_active_v9 as
select m.id
from (select id, superid,ROW_Number() over(partition by id order by id) rank
from temp.match_relation_3M
order by superid desc
) m
where m.rank=1
group by m.id
limit 100