如何在查询中使用average()而不将其添加到groupby - postgresql

yftpprvb  于 2023-10-18  发布在  PostgreSQL
关注(0)|答案(2)|浏览(143)

我有一个层次聚合,使用下面的语句

CREATE MATERIALIZED VIEW IF NOT EXISTS public.values_summary_five_minutes
WITH (timescaledb.continuous,timescaledb.materialized_only = true) AS
SELECT variableid, time_bucket(INTERVAL ‘5 minute’, bucket_interval_one_min) AS bucket_interval_five_min,
MIN(Min_IntValue) as Min_IntValue, MAX(Max_IntValue) as Max_IntValue, SUM(Sum_IntValue) as Sum_IntValue,
COUNT(Count_IntValue) as Count_IntValue, rollup(statsagg_IntValue) as Stats_IntValue, AVG(average(statsagg_IntValue)) as Avg_IntValue
FROM public.values_summary_one_minute_1
GROUP BY variableid, bucket_interval_five_min

在这里,我使用Avg(average(statssummary1d)),这样我就不必将它包含在group by中。但是,该值不正确。是否有其他方法可以替代在分层聚合中找到平均值?

brtdzjyr

brtdzjyr1#

您可以直接使用average(rollup(statsagg_IntValue))demo

CREATE MATERIALIZED VIEW IF NOT EXISTS public.values_summary_five_minutes
WITH (timescaledb.continuous,timescaledb.materialized_only = true) AS
SELECT  variableid, 
        time_bucket(INTERVAL '5 minute', bucket_interval_one_min) AS bucket_interval_five_min,
        MIN(Min_IntValue) as Min_IntValue, 
        MAX(Max_IntValue) as Max_IntValue, 
        SUM(Sum_IntValue) as Sum_IntValue,
        COUNT(Count_IntValue) as Count_IntValue, 
        rollup(statsagg_IntValue) as Stats_IntValue, 
        average(rollup(statsagg_IntValue)) as Avg_IntValue,     --this
        avg(average(statsagg_IntValue)) as Avg_1minAvg_IntValue 
FROM public.values_summary_one_minute_1
GROUP BY variableid, bucket_interval_five_min

| 变量id|铲斗间隔五分钟|最小整数值|max_intvalue|和整数值|count_intvalue| avg_intvalue| avg_1minavg_intvalue|
| --|--|--|--|--|--|--|--|
| 1 |2023-09-27 12:40:00+00| 1 | 5 | 55 | 5 |3.66666666666665| 3 |
给定5个1分钟的区块,其中IntValues如下:

(1),
(2,2),
(3,3,3),
(4,4,4,4),
(5,5,5,5,5)

avg(average(statsagg_IntValue))的结果是3,因为这是个人的平均值,1分钟的平均值。
average(rollup(statsagg_IntValue))应该正确地构建一个包含所有底层IntValues的5分钟区块:

(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)

然后得到3.6666666666666

ippsafx7

ippsafx72#

在PostgreSQL中,当你想计算一个平均值而不将其包含在GROUP BY子句中时,通常使用子查询或窗口函数。在您的情况下,可以使用子查询来确定平均值。以下是如何更改查询以实现此目的:

CREATE MATERIALIZED VIEW IF NOT EXISTS public.values_summary_five_minutes
WITH (timescaledb.continuous, timescaledb.materialized_only = true) AS
SELECT
    variableid,
    bucket_interval_five_min,
    MIN(Min_IntValue) as Min_IntValue,
    MAX(Max_IntValue) as Max_IntValue,
    SUM(Sum_IntValue) as Sum_IntValue,
    COUNT(Count_IntValue) as Count_IntValue,
    rollup(statsagg_IntValue) as Stats_IntValue,
    (SELECT AVG(statsagg_IntValue) FROM public.values_summary_one_minute_1 sub WHERE sub.variableid = main.variableid AND sub.bucket_interval_one_min = main.bucket_interval_five_min) as Avg_IntValue
FROM
    public.values_summary_one_minute_1 main
GROUP BY
    variableid,
    bucket_interval_five_min;

希望它能起作用:)

相关问题