配置单元根据不同条件汇总

时间:2018-11-27 15:03:44

标签: select hive

假设我的表格如下:

cust_id, domain, year, mon, day
1, google.au, 2018, 10, 1
2, virgin.com.au, 2018, 10, 1
3, hotmail.au, 2018, 10, 1
4, yahoo.au, 2018, 10, 1
1, foobar.au, 2018, 10, 1
3, foobar.com.au, 2018, 10, 1
15, haha.com, 2018, 10, 1
11, hehe.net, 2018, 10, 1

我需要按年/月/日分组并根据不同条件汇总列:

1) count of distinct domains ending with .au but not .com.au
2) count of distinct domains ending with .com.au
3) count of distinct hostnames where cust_id in a specific list, let's assume (1, 2, 3, 4)
4) count of all distinct hostnames

所以我的输出如下:

2018, 10, 1, 4, 2, 6, 8

我倾向于针对每个条件使用子查询,然后将它们加入:

select condition_1.year, condition_1.mon, condition_1.day, condition_1.c1, condition_3.c3, condition_4.c4
    from
(select year, mon, day, count(distinct domain) c1 from mytable where year = 2018 and mon = 10 and day = 1
and domain rlike '[.]au' and domain not rlike '[.]com[.]au'
group by year, mon, day) condition_1

full outer join

(select count(distinct domain) c2 from mytable where year = 2018 and mon = 10 and day = 1
and domain rlike '[.]com[.]au') condition_2

full outer join

(select count(distinct domain) c3 from mytable where year = 2018 and mon = 10 and day = 1
and cust_id in (1, 2, 3, 4)) condition_3

full outer join
(select count(distinct hostname) c4 from mytable where year = 2018 and mon = 10 and day = 1) condition_4

这似乎效率很低,尽管我想不出更好的方法。 CASE语句在这里不起作用,因为我需要不同的计数。 我如何才能更有效地实现这一目标?

2 个答案:

答案 0 :(得分:1)

这可以通过正则表达式和条件聚合来实现。

select year,mon,day
,count(distinct case when domain regexp '(?<!\.com)\.au$' then domain end) as ends_with_au
,count(distinct case when domain regexp '\.com\.au$' then domain end) as ends_with_com_au
,count(distinct case when cust_id in (1,2,3,4) then domain end) as specific_cust
,count(distinct domain) as all_domains
from mytable
group by year,mon,day

正则表达式(?<!\.com)\.au$使用否定式后置断言来检查.au之前的字符不是.com$元字符表示匹配.au作为字符串中的最后3个字符。 .必须通过\进行转义。

答案 1 :(得分:0)

使用collect_set()-收集不同的集合,忽略NULL,使用size函数获取元素数量(已经不同):

select
      year, mon, day,
      size(condition_1) as condition_1_cnt,
      size(condition_2) as condition_2_cnt,
      size(condition_3) as condition_3_cnt,
      size(condition_4) as condition_4_cnt    
 from
   (
    select year, mon, day,
       collect_set(case when domain rlike '(?<![.]com)[.]au' then domain end) condition_1,
       collect_set(case when domain rlike '[.]com[.]au'      then domain end) condition_2,
       collect_set(case when cust_id in (1, 2, 3, 4)         then domain end) condition_3,
       collect_set(hostname)                                                  condition_4
      from mytable 
     where year = 2018 and mon = 10 and day = 1
     group by year, mon, day
    )s;