答案 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)