Sql 计算给定ID具有特定值的次数,然后使用算术运算符

Sql 计算给定ID具有特定值的次数,然后使用算术运算符,sql,hive,Sql,Hive,使用Apache在Hive中运行查询,我想计算给定ID拥有订单号的次数,然后只包括至少有3个订单的ID。我使用类似这样的方法来聚合值: select customer_id, count (distinct order_id) from customer_table group by customer_id 只有拥有3个以上订单的客户才能获取id的好方法是什么?我尝试添加带有算术运算符的where子句无法使其工作(例如,其中count(distinct claim_i

使用Apache在Hive中运行查询,我想计算给定ID拥有订单号的次数,然后只包括至少有3个订单的ID。我使用类似这样的方法来聚合值:

    select customer_id, count (distinct order_id) 
    from customer_table
    group by customer_id

只有拥有3个以上订单的客户才能获取id的好方法是什么?我尝试添加带有算术运算符的where子句无法使其工作(例如,
其中count(distinct claim_id)大于等于3

您需要使用
HAVING
子句:

select customer_id, count(distinct order_id) 
from customer_table
group by customer_id
having count(distinct order_id) >= 3

在同一查询中不能有分组依据和不同。 请看打开的蜂箱吉拉

我已经在hive中测试了下面的脚本,它适合我

select customer_id, order_id, count(1) as counting from customer_table
group by customer_id, order_id
having counting >= 3
您是否需要计数(不同)<代码>计数(*)通常更快。