mysql:计算子表中的记录,按日期分组

时间:2017-03-06 10:59:40

标签: mysql

我有2个表,一个包含任务的表和一个表" Tasklog"存储任务进度的地方

Tasks
ID Description 
1  task1
2  task2
3  task3
4  task4
5  task5

任务日志

ID taskId dtTime        Status
1   1       2016-01-01  new
2   1       2016-02-10  in progress
3   1       2016-03-03  closed
4   2       2016-01-01  new
5   2       2016-01-10  in progress
6   2       2016-01-11  closed
7   3       2016-01-01  new 
8   4       2016-01-01  new
9   5       2016-01-01  new
10  5       2016-01-01  in progress

一个任务可以有多个任务日志记录 现在我想创建一个报告来显示有多少任务在哪里打开"每月(开放意味着他们在过去24个月内没有状态"关闭"在那个月内)没有任务日志记录。

所以我需要这样的东西:

2016-01 4
2016-02 4
2016-03 3

我认为这些应该是查询应该产生的实际数字。

我很难写出正确的查询来做到这一点。我想我已经接近了,但是通过下面的查询我计算任务日志记录,而不管他们有多少任务日志记录,我想计算任务记录。

select month(dtTime) month, year(dtTime) year, count(t.id) as number
    from tasklog tl
    join tasks t on t.id = taskId
    where dtTime > DATE_ADD(now(), INTERVAL -2 YEAR)
    and t.id not in ( 
        select id from tasklog tl1 
        where status in ('closed')
        and month(tl1.dtTime) = month(tl.dtTime) 
        and year(tl1.dtTime) = year(tl.dtTime)
    )
    group by month(dtTime), year(dtTime)
    order by dtTime;

有什么建议我最好能做到这一点吗?

创建/填充表格的代码:

CREATE TABLE tasks
    (`id` int, `description` varchar(5))
;

INSERT INTO tasks
    (`id`, `description`)
VALUES
    (1, 'task1'),
    (2, 'task2'),
    (3, 'task3'),
    (4, 'task4'),
    (5, 'task5')
;


CREATE TABLE tasklog
    (`ID` int, `taskId` int, `dtTime` datetime, `Status` varchar(11))
;

INSERT INTO tasklog
    (`ID`, `taskId`, `dtTime`, `Status`)
VALUES
    (1, 1, '2016-01-01 00:00:00', 'new'),
    (2, 1, '2016-02-10 00:00:00', 'in progress'),
    (3, 1, '2016-03-03 00:00:00', 'closed'),
    (4, 2, '2016-01-01 00:00:00', 'new'),
    (5, 2, '2016-01-10 00:00:00', 'in progress'),
    (6, 2, '2016-01-11 00:00:00', 'closed'),
    (7, 3, '2016-01-01 00:00:00', 'new'),
    (8, 4, '2016-01-01 00:00:00', 'new'),
    (9, 5, '2016-01-01 00:00:00', 'new'),
    (10, 5, '2016-01-01 00:00:00', 'in progress')
;

响应Stefano Zanini的更新

似乎两种解决方案都没有给我正确的结果。我相信正确的结果是:

2016-01 4
2016-02 4
2016-03 3

你的查询只给了我:

1   2016    4

如果我将distinct添加到我的查询中,我会得到:

1   2016    5
2   2016    1
3   2016    1 

2 个答案:

答案 0 :(得分:1)

我会选择左联盟

select month(tl.dtTime) month, year(tl.dtTime) year, count(t.id) as number
    from tasks t
    join tasklog tl
    on t.id = tl.taskId and tl.status = 'new'
    left join tasklog tl2
    on t.id = tl2.taskId and month(tl.dtTime) = month(tl2.dtTime) and tl2.status = 'closed'
    where dtTime > DATE_ADD(now(), INTERVAL -2 YEAR)
    and tl2.taskId is null
    group by month(tl.dtTime), year(tl.dtTime)
    order by dtTime;

修改

...但您可能只需要在distinct

中添加count条款
count(distinct t.id)

进一步解释后编辑

我误解了这个要求。这样就可以了:

select  distinct mm.month, count(distinct tl.taskId)
from    (select distinct month(dtTime) mm from tasklog) mm
join    tasklog tl
 on     mm.montId >= month(tl.dtTime) and tl.Status = 'new'
left join
        tasklog tl2
 on     mm.month >= month(tl2.dtTime) and tl2.Status = 'closed' and tl.taskId = tl2.taskId
where   tl2.taskId is null
group by mm.month
order by 1;

第一个加入每个月加入到该月开放的所有任务。对于第二个连接,左边的连接,如果任务在该月或之前的任务中关闭,则为每个月/ taskId提供一个值。从这里,您只能过滤每月的打开任务,过滤没有此值的行。

您必须添加年份并可能优化连接子句,但使用您提供的示例数据,此查询完全没问题。

答案 1 :(得分:0)

这就是我最终的结果:

select  distinct mm.year as year, 
                 mm.month as month, 
                 count(distinct tl.taskId) number
from    (select distinct month(dtTime) month , year(dttime) year  from tasklog) mm
join    tasklog tl
 on     mm.month >= month(tl.dtTime) and mm.year >= year(tl.dtTime) and tl.Status !='closed'
left join
        tasklog tl2
 on     mm.month >= month(tl2.dtTime) and mm.year >= year(tl2.dtTime) and tl2.Status = 'closed' and tl.taskId = tl2.taskId
where   tl2.taskId is null
group by mm.month
order by 1;