在Impala SQL语句中查找总计数

时间:2016-06-24 17:08:43

标签: sql impala

我正在尝试查找已查看特定表中特定对象的所有用户的总数。我有一个查询,到目前为止,它被对象分开......

select object, count(distinct user) 'c' from events e where (action = 'viewed') and object in ('car','truck') group by object, 'c'

输出如下所示......

car 92

truck 20

我想要的是简单地输出数字112。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

在当前查询周围添加sum()

select sum(c)
from
(
    select object, count(distinct user) 'c' 
    from events e 
    where (action = 'viewed') and object in ('car','truck') 
    group by object, 'c'
) tmp

答案 1 :(得分:0)

只需简单地获得您想要的东西:

select count(*) 
from events e 
where action = 'viewed' 
and object in ('car','truck') 

如果您希望按对象分配不同的用户,则可以执行此操作

select count(*) 
from events e 
where action = 'viewed' 
  and object in ('car','truck') 
  and ROW_NUMBER() OVER (PARTITION BY Object, User) = 1