我有一张桌子,my_table:
transaction_id | money | team
--------------------------------------------
1 | 10 | A
2 | 20 | B
3 | null | A
4 | 30 | A
5 | 16 | B
6 | 12 | B
当我按团队分组时,我可以通过查询计算最大值,最小值:
select team, max(money), min(money) from my_table group by team
然而,我不能做avg和sum因为有null。即:
select team, avg(money), sum(money) from my_table group by team
会失败。
有没有办法在计算平均值和总和之前更改列类型?即我希望输出为:
team | avg(money) | sum(money)
--------------------------------------
A | 20 | 40
B | 16 | 48
谢谢!
答案 0 :(得分:1)
Cloudera提供的每个文档您的查询应该按原样运行。 AVG Function和SUM Function https://docs.npmjs.com/how-npm-works/npm3忽略null。
SELECT team, AVG(money), SUM(money)
FROM my_table
GROUP BY team
更新:根据您的评论,我再也不熟悉Impala了。据推测,标准SQL将起作用。您的错误似乎是数据类型问题。
SELECT team, AVG(CAST(money AS INT)), SUM(CAST(money AS INT))
FROM my_table
GROUP BY team
答案 1 :(得分:0)
只需将总和除以计数:
SELECT team, SUM(money)/COUNT(money) AS AVG, SUM(money)
FROM team
GROUP BY team