[英]How to calculate actual average of a column whose column values need to be > than 25% of average in postgresql
I've got 1 year data with million rows in a PostgreSQL 9.2 table, and I'm struggling to calculate one measure used for network statistics. 我在PostgreSQL 9.2表中有100万行的一年数据,而我正努力计算一种用于网络统计的度量。
I have a table with columns date , devicename , traffic_rate
I need to find average traffic_rate excluding holidays . 我需要找到不包括节假日的平均traffic_rate。 A day will be considered holiday if is
traffic_rate is < 25% of average(traffic_rate)
.Again I need to sum the traffic_rate by excluding holidays and find actual average . 如果
traffic_rate is < 25% of average(traffic_rate)
则将一天视为假日。同样,我需要通过排除假日来求和traffic_rate并求出实际平均值。 For eg: 例如:
date devicename traffic_rate
day1 gateway1 45.3
day2 gateway1 1
day3 gateway1 28.2
day4 gateway1 4.56
Average including holidays = (45.3+1+28.2+4.56)/4 = 19.76
Days with traffic_rate < 25 % of 19.76 will be treated as holiday --> Here day2,day4
is treated as holiday 包括
holidays = (45.3+1+28.2+4.56)/4 = 19.76
在内的平均值holidays = (45.3+1+28.2+4.56)/4 = 19.76
当traffic_rate < 25 % of 19.76 will be treated as holiday --> Here day2,day4
天traffic_rate < 25 % of 19.76 will be treated as holiday --> Here day2,day4
被视为假期
Original traffic rate by excluding holidays = (45.3+28.2)/2 = 36.75
不含
holidays = (45.3+28.2)/2 = 36.75
原始流量率holidays = (45.3+28.2)/2 = 36.75
How can i implement this in postgresql query?? 如何在Postgresql查询中实现呢?
Any help will be highly appreciable 任何帮助将是非常可贵的
You could use a common table expression to select the global average first and then use this value to filter the table for the actual query: 您可以使用公用表表达式首先选择全局平均值,然后使用此值为实际查询过滤表:
WITH glob AS ( SELECT 0.25 * AVG( traffic_rate ) as reducedAverage FROM yourTab )
SELECT AVG( yourTab.traffic_rate )
FROM yourTab
INNER JOIN glob
ON yourTab.traffic_rate >= glob.reducedAverage
Try this: 尝试这个:
SELECT
AVG(traffic_rate)
FROM traffic_tbl
WHERE traffic_rate > (SELECT 0.25 * AVG(traffic_rate) FROM traffic_tbl)
Here's another method: 这是另一种方法:
SELECT
AVG(traffic_rate)
FROM
(
SELECT
traffic_rate,
AVG(traffic_rate) OVER () AS avg_traffic_rate
FROM atable
) AS s
WHERE traffic_rate < 0.25 * avg_traffic_rate
;
This solution uses window aggregation ( AVG
with an OVER
clause). 此解决方案使用窗口聚合 (带有
OVER
子句的AVG
)。 The s
derived table contains individual traffic_rate
values together with the average value calculated off the entire table. 该
s
派生表包含个别traffic_rate
值连同关闭整个表中计算出的平均值。 The main query is then uses the average value to filter out rows according to the requirement and calculates the final average based on the remaining values. 然后,主要查询将使用平均值根据需求过滤掉行,并根据剩余值计算最终平均值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.