gpt4 book ai didi

sql - 如何在不分块的情况下按周分组(查询 _trailing_ 4 周)

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

我有一个工作数据库,我想按周计算过去一个月的工作。我当前的查询执行了,但它按日历周而不是我想要的尾随周分块:

SELECT date_trunc('week', scheduled_for::date) AS weekly,
COUNT(*)
FROM jobs_job WHERE state = 'complete' AND scheduled_for > NOW() - INTERVAL '1 month'
GROUP BY weekly
ORDER BY weekly;

我怎样才能改变它,使每个“每周”周期正好是 7 天?

最佳答案

你可能想使用 extract week

SELECT EXTRACT(isoyear FROM scheduled_for) as yearly,
EXTRACT(WEEK FROM scheduled_for) as weekly,
COUNT (*)
FROM jobs_job WHERE state = 'complete' AND scheduled_for > NOW() - INTERVAL '1 month'
GROUP BY EXTRACT(isoyear FROM scheduled_for),
EXTRACT(WEEK FROM scheduled_for)
ORDER BY EXTRACT(isoyear FROM scheduled_for),
EXTRACT(WEEK FROM scheduled_for);

关于sql - 如何在不分块的情况下按周分组(查询 _trailing_ 4 周),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50949856/

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