Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PostgreSQL中,对数组列计算模式(),而不使平均值倾斜_Sql_Postgresql - Fatal编程技术网

在PostgreSQL中,对数组列计算模式(),而不使平均值倾斜

在PostgreSQL中,对数组列计算模式(),而不使平均值倾斜,sql,postgresql,Sql,Postgresql,我设计了一个表来跟踪正在运行的进程: Table "public.processes" Column | Type | Collation | Nullable | Default -------------------------+--------------------------+-----------+----------+--------- id

我设计了一个表来跟踪正在运行的进程:

                                Table "public.processes"
         Column          |           Type           | Collation | Nullable | Default
-------------------------+--------------------------+-----------+----------+---------
 id                      | uuid                     |           | not null |
 duration                | bigint                   |           |          |
 pauses                  | bigint                   |           |          |
 start_date              | timestamp with time zone |           |          |
 end_date                | timestamp with time zone |           |          |
 power_levels            | integer[]                |           |          |
其中pauses是进程拖运的次数,power_levels是一个从0到4的整数数组,允许重复,表示该进程已消耗的功率级别

使用单个查询,我希望选择在某一周内执行的所有流程,并显示:

每周每天的平均持续时间明细 一周内单个进程内的最大暂停次数 一周中最常用的功率级别 因此,给出示例数据:

       start_date       |        end_date        | power_levels | duration | pauses
------------------------+------------------------+--------------+----------+-------
 2020-06-06 10:00:00+00 | 2020-06-06 10:10:00+00 | {3}          |     1000 |     3
 2020-06-07 10:00:00+00 | 2020-06-07 10:10:00+00 | {2}          |     2000 |    10
 2020-06-07 12:00:00+00 | 2020-06-07 12:10:00+00 | {4,1}        |     3000 |    60
 2020-06-08 10:00:00+00 | 2020-06-08 10:10:00+00 | {4,2}        |     4000 |    10
 2020-06-08 12:00:00+00 | 2020-06-08 12:10:00+00 | {4,4,3}      |     1337 |     2
我希望得到类似以下结果:

 most_used_power_level |   avg  | max | dow
-----------------------+--------+-----+-----
                     4 |   2500 |  60 |   0
                     4 | 2668,5 |  60 |   1
                     4 |   1000 |  60 |   6
到目前为止,我已经:

select 
    mode() within group (order by most_used_power), 
    avg(duration),
    max(pauses), 
    extract (dow from start_date) as dow 
from (
    select 
        unnest(power_levels) as most_used_power,
        duration,
        pauses,
        start_date
    from processes 
    where start_date >= '2020-06-01' and start_date < '2020-06-09'
) as foo
group by dow;
但是这个查询有两个问题:

取消子查询中的功率级别测试会扭曲平均值的计算 最后的结果是将最常用的功率级别和每周每天的暂停次数(而不是整个持续时间)分组
除了将查询拆分为2之外,我不确定如何从这里开始。有没有一种方法可以在一次查询中实现这一点?

如果您希望获得与一周中的哪一天无关的最常用功率级别,请分别计算:

select avg(p.duration),
       max(p.pauses), 
       extract(dow from start_date) as dow,
       pl.most_Used_power_level
from processes p cross join
     (select mode() within group (order by power_level) as most_Used_power_level
      from processes p cross join lateral
           unnest(power_levels) power_level
      where p.start_date >= '2020-06-01' and p.start_date < '2020-06-09'
     ) pl
where p.start_date >= '2020-06-01' and p.start_date < '2020-06-09'
group by dow, pl.most_Used_power_level;

请以表格形式提供示例数据和所需结果。如果这是一个愚蠢的问题,很抱歉,但是mod函数不需要第二个参数吗?@noamt。这应该是代码中的模式。