简体   繁体   English

在PostgreSQL中,如何计算其列值必须大于平均值的25%的列的实际平均值

[英]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.76traffic_rate < 25 % of 19.76 will be treated as holiday --> Here day2,day4traffic_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

Example Fiddle 小提琴的例子

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.

相关问题 通过忽略列中的 0 值来计算平均值 - calculate the average by ignoring the 0 values in column 如何计算 TIMESTAMP 列的平均值 - How to calculate an average of TIMESTAMP column 如何计算日期列中的平均值 - How to calculate average in date column 如何计算 Postgresql 中具有相同时间戳的值的平均值? - How to calculate average of values with the same timestamp in Postgresql? 如何计算 BigQuery 中数组列的所有值的平均值和中位数? - How to calculate average and median of all the values of an array column in BigQuery? 从列中获取平均值,然后过滤掉数量小于平均值的所有行,然后计算行数 - Get the average from a column, then filter out all the rows whose number is smaller than the average, and then count the number of row 从JSON列计算平均值 - Calculate average from JSON column 如何计算条件语句中按另一列分组的 mySQL 中的列的平均值? - How to calculate the average of a column in mySQL grouped by another column in a conditional statement? 对于列中的每个字符串计算平均值(第二列) - For each string in column calculate average (second column) 计算每行的平均值,然后计算每列的平均值 - Calculate average per row and then calculate average per column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM