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,我从PostgreSQL获取聚合函数调用不能嵌套错误。我尝试了不同的方法,但都解决不了 select c.*, ( select sum((count(distinct product_id))/2) from page_views where product_id in (c.p1, c.p2) group by user_id, session_id having

我从PostgreSQL获取聚合函数调用不能嵌套错误。我尝试了不同的方法,但都解决不了

select c.*, (
             select sum((count(distinct product_id))/2)
             from page_views
             where product_id in (c.p1, c.p2)
             group by user_id, session_id
             having count(distinct product_id) > 1
            ) freq
from (
      select a.product_id p1, b.product_id p2
      from (select distinct product_id from page_views) a,
           (select distinct product_id from page_views ) b
      where a.product_id <> b.product_id
     ) c ;

谢谢

如果您想统计在一个感官中看到两个页面的用户,那么以下是查询:

select v1.product_id, v2.product_id, count(distinct v2.user_id)
from page_views v1 join
     page_views v2
     on v1.user_id = v2.user_id and v1.session_id = v2.session_id and
        v1.product_id < v2.product_id
group by v1.product_id, v2.product_id;

这是我能想象到的对实际意图最合理的解释。

您可以使用subselect来获取嵌套的聚合函数,如下所示:

select c.*, (SELECT sum(count_column) FROM (
                 select (count(distinct product_id))/2 AS count_column
                 from page_views
                 where product_id in (c.p1, c.p2)
                 group by user_id, session_id
                 having count(distinct product_id) > 1
               ) sub_q
            ) freq
from (
      select a.product_id p1, b.product_id p2
      from (select distinct product_id from page_views) a,
           (select distinct product_id from page_views ) b
      where a.product_id <> b.product_id
     ) c ;

我不完全理解您在这个示例中试图做什么,但是我想展示一个示例,说明我如何在PostgresQL中使用多个聚合函数

假设我的目标是找到maxtime-mintime的最大值:

select max(a.trip_time) from 
(  select trip_id, max(time) - min(time) as trip_time 
   from gps_data 
   where date = '2019-11-16' 
   group by trip_id) as a;

我希望这是清楚的

请解释你想做什么。不起作用的SQL没有必要很好地解释意图。请回答您的问题,并添加一些示例数据和基于该数据的预期输出。请