gpt4 book ai didi

sql - 比较同一组 SQL 中的 2 个时间段

转载 作者:行者123 更新时间:2023-11-29 13:28:31 25 4
gpt4 key购买 nike

我正在尝试聚合一些数据,按特定列对其进行排序,然后将相同数据与另一个时间段(例如上周)进行比较。

例如,本周点击率最高的网站:

url            total hits
www.a.com 10,000
www.b.com 9,0000
www.e.com 5,000

这是按点击率排序的,但我想获得相同 url 的点击率,但时间段不同,例如:

url            total-hits   total-hits (last week)
www.a.com 10,000 8,000
www.b.com 9,0000 6,000
www.e.com 5,000 6,000

此数据的表格布局是页面点击列表,例如:

hit_table:

id timestamp url
1 1426470088 www.a.com
1 1426470000 www.b.com
1 1426468015 www.c.com
1 1426467000 www.b.com
....

是否可以在单个 SQL 查询中执行此操作,或者我是否需要为此进行 2 个单独的查询?

最佳答案

您可能可以使用条件聚合来做您想做的事。你的问题在表的结构上非常模糊,但这是想法:

select url,
sum(case when timestamp >= current_date - interval '7 day' then 1 else 0 end) as ThisWeek,
sum(case when timestamp >= current_date - interval '14 day' and
timestamp < current_date - interval '7 day'
then 1 else 0 end) as ThisWeek
from table t
group by url;

关于sql - 比较同一组 SQL 中的 2 个时间段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29068344/

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