Postgresql 查询用于创建分组、聚合和筛选行集的不同计数
我有一张这样的桌子:Postgresql 查询用于创建分组、聚合和筛选行集的不同计数,postgresql,amazon-redshift,Postgresql,Amazon Redshift,我有一张这样的桌子: control=# select * from animals; age_range | weight | species -----------+--------+--------- 0-9 | 1 | lion 0-9 | 2 | lion 10-19 | 2 | tiger 10-19 | 3 | horse 20-29 | 2 | tiger 20-29
control=# select * from animals;
age_range | weight | species
-----------+--------+---------
0-9 | 1 | lion
0-9 | 2 | lion
10-19 | 2 | tiger
10-19 | 3 | horse
20-29 | 2 | tiger
20-29 | 2 | zebra
我执行了一个查询,该查询汇总了年龄范围组内动物的权重,我只想返回聚合权重大于的行
一定数量
摘要查询:
SELECT
age_range,
SUM(animals.weight) AS weight,
COUNT(DISTINCT animals.species) AS distinct_species
FROM animals
GROUP BY age_range
HAVING SUM(animals.weight) > 3;
总结结果:
age_range | weight | distinct_species
-----------+--------+------------------
10-19 | 5 | 2
20-29 | 4 | 2
现在麻烦来了。除了这个摘要之外,我还想报告用于创建上述摘要行集的物种的不同数量。为简单起见,我们将此数字称为“不同物种总数”。在这个简单的例子中,由于只有3个物种(老虎、斑马、马)用于产生本摘要的2行,而不是“狮子”,因此“不同物种总数”应为3。但我不知道如何成功地查询那个号码。由于摘要查询必须使用having子句才能将筛选器应用于已分组和聚合的行集,因此在尝试查询“Distinct SECTION Total”时会出现问题
这将返回错误的数字2,因为它是不正确的不重复计数:
SELECT
COUNT(DISTINCT distinct_species) AS distinct_species_total
FROM (
SELECT
age_range,
SUM(animals.weight) AS weight,
COUNT(DISTINCT animals.species) AS distinct_species
FROM animals
GROUP BY age_range
HAVING SUM(animals.weight) > 3
) x;
当然,它返回错误的数字,4,因为它不考虑使用一个有句:过滤分组和汇总的摘要结果。
SELECT
COUNT(DISTINCT species) AS distinct_species_total
FROM animals;
非常感谢您能为我提供任何帮助,帮助我走上正确的道路,并希望能帮助其他有类似问题的人,但最终我确实需要一个能与Amazon Redshift一起工作的解决方案。将结果集与原始动物表结合起来,并统计不同的物种
select distinct x.age_range,x.weight,count(distinct y.species) as distinct_species_total
from
(
select age_range,sum(animals.weight) as weight
from animals
group by age_range
having sum(animals.weight) > 3
) x
join animals y on x.age_range=y.age_range
好的,干净的溶液。实际上,我的“动物”表是一个派生表,由多个表联接产生,但您的解决方案也适用于此。