我有一个问题是,是否可以通过聚合函数创建一个组。
场景:
我有一张桌子,其中包含生物量(kg)和每天的个人数量以及描述,因此我可以计算总的av。两个日期内的体重和总人数:
select
description,
sum(biomass)/sum(number_individuals) as av.weight,
sum(number_individuals) as individuals
from
Table
group by description
哪个有效,现在,问题是我想将那些按重量范围分开的人分组,以获得类似的东西:
description range(kg) number av.weigh(g)
Foo 2-3 2400 2584.48
我尝试了类似
的内容SELECT
description,
case when sum(biomass)/sum(number_individuals) >= 2000.0
and sum(biomass)*1000/sum(number_individuals) < 3000 then '2-3'
else 'nothing'
end as desc_range
FROM Table
Group by
description,
sum(biomass)/sum(number_individuals)
但它似乎没有用,也没有使用别名desc_range ofc。
我正在使用Informix 9.40 TC3
任何帮助将不胜感激。
祝你好运
答案 0 :(得分:1)
如果要在聚合上聚合,通常需要子查询。但是,你提到个人,所以也许这就是你想要的:
select description,
(case when biomass between 2 and 3 then '2-3'
else 'nothing'
end) as biomass
sum(biomass)/sum(number_individuals) as av.weight, sum(number_individuals) as individuals
from Table
group by description,
(case when biomass between 2 and 3 then '2-3'
else 'nothing'
end);