在 impala 中创建一个包含每日计数的列

Create a column with daily count in impala

我想创建一个计数列,其中包含每天的计数。我设法做到了:

select book, orders, s.common_id,s.order_date,d.customer_region,t.cnt
from books_tbt as s
inner join  customer_tbt as d
on s.common_id = d.common_id
inner join (select count(*) as cnt,order_date from customer_tbt where customer !='null' 
group by order_date) as t
on t.order_date = d.order_date
where d.customer !='null'
and s.order_date = 20220122
group by book, orders, s.common_id,s.order_date,d.customer_region,t.cnt;

请问有没有更高效的方法?

您可以简单地使用 COUNT(*) OVER( Partitioned by ORDER_DATE Order by ORDER_DATE) window 函数来计算订单日期的数量。

select book, orders, s.common_id,s.order_date,d.customer_region,d.cnt
from books_tbt as s
inner join  
( select d.*, COUNT(*) OVER( Partition by ORDER_DATE Order by ORDER_DATE) as cnt from customer_tbt d) as d on s.common_id = d.common_id -- count(*) over can not be calculated together with group by so we are using a sub qry
where d.customer !='null'
and s.order_date = 20220122
group by book, orders, s.common_id,s.order_date,d.customer_region,d.cnt;