gpt4 book ai didi

php - 如何编辑此 MySQL 查询以包含带有计数和平均值的额外列?

转载 作者:行者123 更新时间:2023-11-29 18:11:23 24 4
gpt4 key购买 nike

此查询:

select p.visitor_id , p.url, avg(p.duration), count(*) as sum_pages
from pages p
where visitor_id = 1
group by p.visitor_id , p.url
having avg(p.duration) > (select avg(p2.duration)
from pages p2
where p2.visitor_id = p.visitor_id
);

生成这个假设的表格:

visitor_id ---- url ---- duration --- sum_pages
1 ------------ home ------- 5 ------- 20
1 ------------ about ------ 8 ------- 12
1 ------------ contact ---- 2 ------- 13
1 ------------ services --- 2 ------- 18

它的作用:获取整个表的平均持续时间数,并检查平均持续时间 URL 是否更大。如果是,则显示号码。

我想做同样的事情,这次是添加综合浏览量。每行都算作一次综合浏览量。

如何实现这一目标?

最佳答案

我找到了一种方法来实现此目的,但不确定查询的性能:

select
visitor_id,
url,
sum(duration),
avg(duration),
count(*) as sum_pages
from pages p
where visitor_id='1'
group by visitor_id,url
having sum(duration) > (select avg(duration)
from pages
where visitor_id=p.visitor_id
)
and sum_pages > (select count(*)/(select count(distinct url) from pages where visitor_id=p1.visitor_id)
from pages p1
where p1.visitor_id=p.visitor_id
)
;

DEMO

关于php - 如何编辑此 MySQL 查询以包含带有计数和平均值的额外列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com