Hive 在配置单元查询中获取不同的字段

Hive 在配置单元查询中获取不同的字段,hive,hiveql,Hive,Hiveql,我正在尝试查询一个表并对查询结果进行重复数据消除 select distinct(id), distinct(name), distinct(date) from table where data_date = '20180722' and active = True group by market, country order by userrole 我要做的是给我一个不同id、名称、日期等的列表,这是每个分组市场、国家/地区组的第一行,它们首先按用户角色排序。在hive中是否有这样做的方法

我正在尝试查询一个表并对查询结果进行重复数据消除

select distinct(id), distinct(name), distinct(date)
from table
where data_date = '20180722'
and active = True
group by market, country
order by userrole
我要做的是给我一个不同id、名称、日期等的列表,这是每个分组市场、国家/地区组的第一行,它们首先按用户角色排序。在
hive
中是否有这样做的方法

R
中,这将是:

df %>%
    select(id, name, date) %>%
    group_by(market, country) %>%
    arrange(userrole) %>%
    slice(1)

SQL和HiveQL中的
distinct
不是一个函数,而是一个关键字。在每个查询中指定
distinct
一次将得到不同的结果,如
选择distinct col1、col2…

您可以使用
row\u number
功能来实现这一点

select id,name,date
from (select t.*,row_number() over(partition by market,country order by userrole) as rnum
      from tbl t
     ) t
where rnum=1

你肯定是在使用同一个查询吗?这很有效!我不得不添加一些东西,并意识到我做得有点错误。非常感谢。为什么您不必将选择部分别名为
t.id、t.name、t.date
t.rnum=1
?那总是让我感到厌烦。我以为您在给子表添加别名,使其名为
“t”
。是的。因为所有列都来自同一别名,并且没有涉及其他表。不使用它应该没问题。它类似于从表中选择col1和col2