Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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
如何在SQL中使用查询过分区来获得当前值、平均值和最大值?_Sql_Postgresql_Window Functions - Fatal编程技术网

如何在SQL中使用查询过分区来获得当前值、平均值和最大值?

如何在SQL中使用查询过分区来获得当前值、平均值和最大值?,sql,postgresql,window-functions,Sql,Postgresql,Window Functions,我有一张表,其中显示了设备在某个区域和特定位置执行的一个点 working_date device points area location 19-06-2020 a 1 x xa 19-06-2020 a 2 x xa 19-06-2020 a 3 x xa 19-06-2020 a

我有一张表,其中显示了设备在某个区域和特定位置执行的一个点

working_date    device   points   area   location
19-06-2020        a        1       x       xa   
19-06-2020        a        2       x       xa 
19-06-2020        a        3       x       xa 
19-06-2020        a        4       x       xa
20-06-2020        a        5       x       xa
20-06-2020        a        6       x       xa
20-06-2020        a        7       x       xa
20-06-2020        a        8       x       xa
20-06-2020        a        9       x       xa
我想得到当前,平均值和最大值,按区域和位置分组。如果我选择任何一天,当前数量将显示最近工作日期的数量。同时,“平均数量”将显示设备工作的总体平均值。最后,最大数量将显示设备完成的总体最大点

根据上表,如果我选择21-06-2020,则预期结果:

working_date  area  location   device   current_qty  avg_qty   max_qty
21-06-2020     x       xa        a         5           4,5        5
平均数量来自日期的总数量/总数量,而最大数量来自所有日期的最大数量

到目前为止,我构建的查询是:

select t1.working_date, t1.device, t1.area, t1.location, t1.points_qty, t1.total_date,
sum(t1.pile_qty) over(partition by t1.working_date) / sum(t1.total_date) over(partition by t1.working_date) as avg_qty,
max(t1.pile_qty) over(partition by t1.working_date) as max_qty
from (
select working_date, device, points, area, location, count(points) as points_qty, count(distinct working_date) as total_date 
from table1 group by device, area, location
group by working_date, device, points, area, location) t1
group by working_date, device, points, area, location, pile_qty, total_date
通过上述查询,我得到:

working_date  area  location   device   current_qty  avg_qty   max_qty
21-06-2020     x       xa        a         5           5          5
我应该如何编写查询以获得所需的结果

提前谢谢


然而,在这种情况下,还不清楚2020-06-20组的五项记录中应该取哪一项。您必须应用您的订购标准,才能将预期的订单订购到顶部。

我想,我有解决方案可供您选择。然而,我不确定答案是否能在不同的场景中提供正确的结果。下面是我的代码=> 请检查链接=>


注意:-在这里,您可以看到,对于当前数量,我使用了您的输入日期21-06-2020,如从CTE选择GRP计数,其中工作日期您所需的输出显示21,这在示例数据中不可用。因此,很难理解计算出的数据……这意味着,如果我选择任何一天,所需的结果将始终为我提供最新的工作日期数量值从何而来?通过计算点,我不知道PostgreSQL,但在MS SQL Server中,聚合函数返回的数据类型与聚合列相同;我建议使用sum…*1.0/总和…答案有帮助吗?那请别忘了给他们投票!如果一个答案完全解决了您的问题,请不要忘记接受它来结束问题。
SELECT
    *,
    AVG(current_qty) OVER () as avg_qty,             -- 2
    MAX(current_qty) OVER () as max_qty
FROM (
    SELECT 
        working_date,
        area,
        location,
        device,
        COUNT(*) as current_qty                      -- 1
    FROM mytable
    GROUP BY working_date, device, area, location    -- 1
) s
WHERE working_date <= '2020-06-21'                   -- 3
ORDER BY working_date DESC
LIMIT 1
SELECT
    *,
    AVG(current_qty) OVER () as avg_qty,
    MAX(current_qty) OVER () as max_qty
FROM (
    SELECT 
        working_date,
        area,
        location,
        device,
        COUNT(*) OVER (PARTITION BY working_date) as current_qty
    FROM mytable
) s
WHERE working_date <= '2020-06-21'
ORDER BY working_date DESC
LIMIT 1
WITH CTE AS
    (
      SELECT working_date,area,location,device, 
             COUNT(working_date) GrpCount
      FROM MYTable 
      GROUP BY working_date,area,location,device
    
    ),y AS
    (SELECT area,location,device,GrpCount,
           (SELECT GrpCount FROM CTE WHERE working_date<TO_DATE('21-06-2020','DD-MM-YYYY') ORDER BY working_date DESC LIMIT 1)  current_qty  
    FROM CTE
    )
    SELECT TO_DATE('21-06-2020','DD-MM-YYYY'),area,location,device, 
           MAX(current_qty) current_qty,
           string_agg(GrpCount::text, ',') avg_qty,
           Max(GrpCount) max_qty
    FROM Y
    GROUP BY area,location,device