获取开放和关闭的帖子按日期计算,间隔为1周?

时间:2016-06-27 06:31:30

标签: php mysql

我需要按照日期和1周的时间间隔分组关闭和开放帖子的帖子数量。我现在得到的是。

yProperty

我想要的就是这样。

enter image description here

抱歉解释不好。 谢谢!

2 个答案:

答案 0 :(得分:3)

请试一试:

SELECT 
date_posts - INTERVAL WEEKDAY(date_posts) DAY AS date_start,
date_posts + INTERVAL (6 - WEEKDAY(date_posts)) DAY AS date_start,
COUNT(*) totalPosts,
COUNT(CASE WHEN Close_count = 1 THEN 1 END) closeCount,
COUNT(CASE WHEN Open_count = 1 THEN 1 END) openCount
FROM your_table
GROUP BY YEARWEEK(date_posts,1);

注意:

YEARWEEK(date)函数假定开始日期为Sunday

YEARWEEK(date,1)函数假定开始日期为Monday

WEEKDAY(date)函数返回相应周的日期索引,假设Monday为一周的开始日期。它会返回0的{​​{1}},Monday的{​​{1}}和1的{​​{1}}。

修改

日期范围搜索:

Tuesday

所以,基于此,如果你想获取过去5周的结果,包括当前周,你需要运行以下查询:

6

答案 1 :(得分:0)

SELECT DATE(i.time_added - INTERVAL WEEKDAY(i.time_added)DAY)  AS date_start,
             DATE(i.time_added + INTERVAL (6 - WEEKDAY(i.time_added))DAY ) AS date_end,
                        COUNT(DISTINCT i.lead_id) AS Leads_count,
                         COUNT(DISTINCT CASE WHEN i.`stage` IN
                         ('6','8') THEN i.`lead_id`
                         ELSE NULL END) AS Close_count
                        ,COUNT(DISTINCT CASE WHEN i.`stage` IN ('1','2','3','4','5','7','9')
                         THEN i.`lead_id` ELSE NULL END) AS Open_count
                FROM tbl i
                INNER JOIN tbl s ON i.stage = s.status_id
                    INNER JOIN tbl u ON i.assigned_to = u.userid
                    INNER JOIN tbl p ON p.purpose_id = i.purpose
                 WHERE 1=1 AND i.STATUS = 'on' AND i.time_added >= DATE_SUB(NOW(),INTERVAL 5 WEEK) - 
    INTERVAL WEEKDAY(DATE_SUB(NOW(),INTERVAL 5 WEEK)) DAY 
                 AND i.time_added <= NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY 
                  GROUP BY YEARWEEK(i.time_added,1)