我正在尝试创建一个查询,它将为我提供打开的门票数量&在按周分组的指定时间范围内关闭。有没有人有关于如何实现这一目标的想法?
如果我从T1.closed
而不是T1.opened
更改时间参考,我会为一列而不是另一列进行正确的分组。
以下是我到目前为止的情况。
SELECT
FROM_DAYS(TO_DAYS(T1.created) -MOD(TO_DAYS(T1.created) -1, 7)) as week,
COUNT(T1.created) as Opened, COUNT(T2.ticket) as Closed
FROM issues as T1
LEFT JOIN issues T2 on T1.ticket = T2.ticket AND T1.closed
BETWEEN (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
AND (DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AND T1.closed IS NOT NULL
WHERE
T1.created > (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
AND
T1.created < (DATE_SUB(CURDATE(), INTERVAL 1 MONTH))
GROUP BY
week
答案 0 :(得分:0)
据我所知t1.opened和t1.closed是日期,所以你可以选择:
select `week`, sum(`opened`), sum(`closed`) from
(
select week(t1.opened) as `week`, count(*) as `opened`, 0 as `closed`
from issues as t1
where t1.opened is not null AND ##FILTER BY OPENED##
group by week(t1.opened)
union all
select week(t2.closed) as `week`, 0 as `opened`, count(*) as `closed`
from issues as t2
where t2.closed is not null AND ##FILTER BY CLOSED##
group by week(t2.closed)
) as t3
group by `week`
注意:您看到有2个查询,请不要加入,因为可能有数周没有关闭/打开的问题
注意2:如果你想按创建日期添加过滤器 - 你需要考虑它应该如何工作,例如问题是在1月1日创建的,1月8日开放,1月15日关闭,应该生成以下每个范围: 1-7,2-8,9-16?