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

标签 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 

最佳答案

我会选择左连接

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;

编辑

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

中添加一个 distinct 子句
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 赋值。从这里您可以仅过滤每个月的未完成任务,过滤没有此值的行。

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

关于mysql:计算子表中的记录,按日期分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42623848/

相关文章:

mysql - 一张表 MYSQL 内连接多列

php - mysql select 查询中忽略#,\,/,大写,$

python - 在 Redis 中存储数据然后获取该数据

php - 全文mysql搜索并匹配精确的字符串

MySQL更新表以填充另一个表中的空值

mysql - 合并两个表

php - 上一个 - mysql 中的下一个查询

mysql - 将其用作值之前的 SQL 处理字符串

php - 从android发送数据到mysql

php - 一个应用程序的多个数据库连接