配置单元中的多个select语句

时间:2016-04-07 10:21:48

标签: hive

toi对蜂巢来说相当新鲜。 我有一个名为stats的表,如下所示

from_date  |  to_date   |   customer_name |  callcount
-------------------------------------------------------
2016_01_01 | 2016_01_02 |  ABC            | 25  
2016_01_02 | 2016_01_03 |  ABC            | 53  
2016_01_03 | 2016_01_04 |  ABC            | 44  
2016_01_04 | 2016_01_05 |  ABC            | 55

我想构建一个hive查询将接受: a)当前时间范围(从和到时间) b)之前的时间范围(从和到时间) c)客户名称

例如:输入将是: 当前时间范围可以是从时间(2016_01_03)到时间(2016_01_05) 当前时间范围可以是从时间(2016_01_01)到时间(2016_01_02) 客户名称可以是ABC

我要显示的结果是: current_call_count(当前时间范围的呼叫计数总和),previous_call_count(前一时间范围的呼叫计数总和) 和current_call_count&之间的区别previous_call_count

像这样:

customer | current_call_count | previous_call_count | Diff
---------------------------------------------------------
ABC      | 99                 | 25                  | 74

我建立的查询是:

select * from 
(
select sum(callCount) as current_count from stats 
where customer_name='ABC' and from_date>='2016-04-03' and to_date<='2016-04-05'
UNION ALL
select sum(callCount) as current_count from stats 
where customer_name='ABC' and from_date>='2016-04-01' and to_date<='2016-04-02'
  ) FINAL

我无法进行计算,也无法将结果显示为列。请帮忙

1 个答案:

答案 0 :(得分:0)

尝试条件聚合:

select 
    sum(case when from_date >= '2016_01_04' and to_date <= '2016_01_05' then callcount else 0 end)
    as current_call_count,
    sum(case when from_date >= '2016_01_02' and to_date <= '2016_01_03' then callcount else 0 end)
    as previous_call_count,
    sum(case when from_date >= '2016_01_04' and to_date <= '2016_01_05' then callcount else 0 end)
    - sum(case when from_date >= '2016_01_02' and to_date <= '2016_01_03' then callcount else 0 end)
    as difference
from stats
where customer_name = 'ABC'

请注意,您的示例数据使用_代替-(在您的查询中使用)作为日期分隔符。