如何按排序的顺序获取数组中的数字计数,然后将其累加为蜂巢?

时间:2018-10-23 04:40:30

标签: hive

我有下表

select * from teller;
Saeed   ["20","5","5","10","1"]
Joe     ["1","1","5","1","10",]
Henry   ["1","1","1","1","1","5","10"]
Jon     ["1","1","1","1","1","5","10"]

以下命令让我获取每个数字的计数:

  Select denomination, count(*) FROM teller LATERAL VIEW explode(bill) subs 
  as denomination GROUP By denomination;

我的输出是

1       77
10      73
20      23
5       35

我想我的输出按面额排序,我尝试过:

  Select denomination, count(*) FROM teller LATERAL VIEW explode(bill) subs 
  as denomination ORDER By denomination;

我收到此错误: 表达式不在GROUP BY键的“面额”中

我如何对其进行排序,以及如何求和?

1 个答案:

答案 0 :(得分:0)

只需在查询中的group by之前添加一个order by,它就会停止为您提供错误提示。

Select denomination, count(*) 
FROM teller LATERAL VIEW explode(bill) subs as denomination 
GROUP BY denomination
ORDER By denomination;

如果想要上述结果的总和,则可以执行以下操作:

Select SUM(cnt)
FROM 
(Select denomination, count(*) as cnt
FROM teller LATERAL VIEW explode(bill) subs as denomination 
GROUP BY denomination
ORDER By denomination
)a
;