在 Impala SQL 语句中查找总计数

Finding a total count in Impala SQL statement

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

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。任何帮助都会很棒。

在您当前的查询周围加上 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

只为更简单地获得你想要的:

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