如何计算雅典娜(Presto)中的总数百分比?
How to calculate percentage of total in Athena (Presto)?
给定一个 table 列,例如:
Date, Type
我是运行以下SQL:
SELECT Type, count(*) as CountPerType
FROM myTable
WHERE Date between 20200101 and 20200131
GROUP BY count(*)
我想要一个额外的列 Percentage
,其中将包含 100.0 * CountPerType / SUM(CountPerType)
。在 PrestoDB(支持 Amazon Athena)中最有效的方法是什么?
您可以使用 window 功能来实现这一点。您应该始终对非聚合字段进行分组。
select
Type,
CountPerType,
100.0 * CountPerType/sum(CountPerType) over () as columnName
from
(
SELECT
Type,
count(*) as CountPerType
FROM myTable
WHERE Date between 20200101 and 20200131
GROUP BY
Type
) subq
我会编写没有子查询的查询。您可以混合使用 window 函数和聚合函数:
SELECT Type, COUNT(*) as CountPerType,
COUNT(*) * 100.0 / SUM(COUNT(*)) OVER () as percentage
FROM t
WHERE Date BETWEEN 20200101 AND 20200131
GROUP BY Type;
我不知道性能是否与使用子查询的版本不同(至少应该一样好)。但是查询肯定更简单。
给定一个 table 列,例如:
Date, Type
我是运行以下SQL:
SELECT Type, count(*) as CountPerType
FROM myTable
WHERE Date between 20200101 and 20200131
GROUP BY count(*)
我想要一个额外的列 Percentage
,其中将包含 100.0 * CountPerType / SUM(CountPerType)
。在 PrestoDB(支持 Amazon Athena)中最有效的方法是什么?
您可以使用 window 功能来实现这一点。您应该始终对非聚合字段进行分组。
select
Type,
CountPerType,
100.0 * CountPerType/sum(CountPerType) over () as columnName
from
(
SELECT
Type,
count(*) as CountPerType
FROM myTable
WHERE Date between 20200101 and 20200131
GROUP BY
Type
) subq
我会编写没有子查询的查询。您可以混合使用 window 函数和聚合函数:
SELECT Type, COUNT(*) as CountPerType,
COUNT(*) * 100.0 / SUM(COUNT(*)) OVER () as percentage
FROM t
WHERE Date BETWEEN 20200101 AND 20200131
GROUP BY Type;
我不知道性能是否与使用子查询的版本不同(至少应该一样好)。但是查询肯定更简单。