简体   繁体   English

在 postgres/redshift 中对百分比计算进行分组

[英]Grouping a percentage calculation in postgres/redshift

I keep running in to the same problem over and over again, hoping someone can help...我一遍又一遍地遇到同样的问题,希望有人能帮忙......

I have a large table with a category column that has 28 entries for donkey breed, then I'm counting two specific values grouped by each of those categories in subqueries like this:我有一个大表,其中有一个类别列,其中包含 28 个驴品种条目,然后我计算两个特定值,这些值按子查询中的每个类别分组,如下所示:

WITH totaldonkeys AS (
    SELECT donkeybreed,
           COUNT(*) AS total
    FROM donkeytable1
    GROUP BY donkeybreed
)
,
sickdonkeys AS (
    SELECT donkeybreed,
           COUNT(*) AS totalsick
    FROM donkeytable1
    JOIN donkeyhealth on donkeytable1.donkeyid = donkeyhealth.donkeyid
    WHERE donkeyhealth.sick IS TRUE
    GROUP BY donkeybreed
)
,

It's my goal to end up with a table that has primarily the percentage of sick donkeys for each breed but I always end up struggling like hell with the problem of not being able to group by without using an aggregate function which I cannot do here:我的目标是最终得到一个表格,该表格主要包含每个品种的病驴百分比,但我总是在不使用聚合 function 的情况下无法分组的问题上苦苦挣扎,我在这里做不到:

SELECT (CAST(sickdonkeys.totalsick AS float) / totaldonkeys.total) * 100 AS percentsick,
                totaldonkeys.donkeybreed
    FROM totaldonkeys, sickdonkeys
    GROUP BY totaldonkeys.donkeybreed

When I run this I end up with 28 results for each breed of donkey, one correct I believe but obviously hundreds of useless datapoints.当我运行这个程序时,我最终得到了每种驴子的 28 个结果,我相信一个是正确的,但显然有数百个无用的数据点。

I know I'm probably being really dumb here but I keep hitting in to this same problem again and again with new donkeydata, I should obviously be structuring the whole thing a new way because you just can't do this final query without an aggregate function, I think I must be missing something significant.我知道我在这里可能真的很愚蠢,但是我一直在用新的 donkeydata 一次又一次地遇到同样的问题,我显然应该以一种新的方式来构建整个事情,因为你不能在没有聚合的情况下完成这个最终查询function,我想我一定遗漏了一些重要的东西。

You can easily count the proportion that are sick in the donkeyhealth table您可以轻松计算donkeyhealth表中的患病比例

SELECT d.donkeybreed,
       AVG( (dh.sick)::int ) AS proportion_sick
FROM donkeytable1 d JOIN
     donkeyhealth  dh
     ON d.donkeyid = dh.donkeyid
GROUP BY d.donkeybreed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM