在结果中,得到一些依赖

时间:2016-02-15 10:03:05

标签: mysql

我有这个MySQL DB:

enter image description here

MySQL为了创建DB +测试值集:

DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
CREATE TABLE table_status ( Id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Status CHAR(27) NOT NULL) ENGINE=INNODB;
INSERT INTO table_status (Id, Status) VALUES (1, 'CREATED'), (2, 'UNSURE'), (3, 'RUNNING'), (4, 'BEGIN_ACTION_TRUE'), (5, 'STOPPED_ACTION_TRUE_OK'), (6, 'STOPPED_ACTION_TRUE_NOT_OK'), (7, 'BEGIN_ACTION_FALSE'), (8, 'STOPPED_ACTION_FALSE_OK'), (9, 'STOPPED_ACTION_FALSE_NOT_OK'), (10, 'STOPPED_NOT_OK'), (11, 'STOPPED_OK'), (12, 'CANCEL_REQUESTED'), (13, 'CANCELLED');
CREATE TABLE table_group ( Id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Status TINYINT UNSIGNED NOT NULL DEFAULT 1, CONSTRAINT fk_group_Status FOREIGN KEY (Status) REFERENCES table_status(Id)) ENGINE=INNODB;
CREATE TABLE table_task ( Id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, IdGroup BIGINT UNSIGNED NOT NULL, Status TINYINT UNSIGNED NOT NULL DEFAULT 1, Command TEXT NOT NULL, CONSTRAINT fk_task_IdGroup FOREIGN KEY (IdGroup) REFERENCES table_group(Id) ON DELETE CASCADE, CONSTRAINT fk_task_Status FOREIGN KEY (Status) REFERENCES table_status(Id)) ENGINE=INNODB;
CREATE TABLE table_dependency ( IdBeforeTask BIGINT UNSIGNED NOT NULL, IdAfterTask BIGINT UNSIGNED NOT NULL, PRIMARY KEY (IdBeforeTask, IdAfterTask), CONSTRAINT fk_dependency_IdBeforeTask FOREIGN KEY (IdBeforeTask) REFERENCES table_task(Id) ON DELETE CASCADE, CONSTRAINT fk_dependency_IdAfterTask FOREIGN KEY (IdAfterTask) REFERENCES table_task(Id) ON DELETE CASCADE) ENGINE=INNODB;
INSERT INTO table_group (Id) VALUES (1), (2), (3), (4);
INSERT INTO table_task (IdGroup, Command) VALUES (1, "command_group_1_task_1"), (1, "command_group_1_task_2"), (1, "command_group_1_task_3"), (2, "command_group_2_task_1"), (2, "command_group_2_task_2"), (2, "command_group_2_task_3"), (3, "command_group_3_task_1"), (3, "command_group_3_task_2"), (3, "command_group_3_task_3"), (4, "command_group_4_task_1"), (4, "command_group_4_task_2"), (4, "command_group_4_task_3");
INSERT INTO table_dependency (IdBeforeTask, IdAfterTask) VALUES (1, 2), (2, 3), (4, 5), (5, 6), (7, 8), (8, 9), (10, 11), (11, 12);

table_status中的数据固定为:

INSERT INTO table_status (Id, Status) VALUES 
    (1, 'CREATED')
    (2, 'UNSURE')
    (3, 'RUNNING')
    (4, 'BEGIN_ACTION_TRUE')
    (5, 'STOPPED_ACTION_TRUE_OK')
    (6, 'STOPPED_ACTION_TRUE_NOT_OK')
    (7, 'BEGIN_ACTION_FALSE')
    (8, 'STOPPED_ACTION_FALSE_OK')
    (9, 'STOPPED_ACTION_FALSE_NOT_OK')
    (10, 'STOPPED_NOT_OK')
    (11, 'STOPPED_OK')
    (12, 'CANCEL_REQUESTED')
    (13, 'CANCELLED')

目的是让一个包含任务的组,每个任务可以依赖另一个任务。

我想要获得:

Group.Id,NB_TASK_TOTAL,NB_TASK_CREATED,NB_TASK_UNSURE

但是有一个陷阱:NB_TASK_CREATED的数量定义为:状态为CREATED并且没有未解决的依赖关系的任务数。

未解决的依赖关系被定义为任务取决于另一个具有状态!= 11(STOPPED_OK)的任务。

所以,如果我有这个:

table_dependency : 

IdBeforeTask    IdAfterTask
    1              2
    2              3

table_task :

Id  IdGroup     Status  Command
1     1           1     command_1
2     1           1     command_2
3     1           1     command_3

我想要

Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
 1          3                 1                0

这是我的不成功的尝试:

SELECT

G.Id,

(SELECT COUNT(T.Id) FROM table_task as T WHERE T.IdGroup = G.Id),
(SELECT COUNT(T.Status = 1) FROM table_task as T WHERE T.IdGroup = G.Id AND T.Id NOT IN (SELECT IdAfterTask from table_dependency as D1 join table_task as T1 on T1.Id=D1.IdBeforeTask WHERE T1.Status!=11)),

(SELECT COUNT(T.Status = 2) FROM table_task as T WHERE T.IdGroup = G.Id)

FROM

table_group as G

问题是我得到了这个:

Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
 1          3                 1                3
 2          3                 0                3

(我有4个小组,每个小组包含3个任务,所有任务的状态= 1(CREATED)和依赖关系设置,以便刚刚解决"依赖所以我应该

Group.Id, NB_TASK_TOTAL, NB_TASK_CREATED, NB_TASK_UNSURE
 1          3                 1                0
 2          3                 1                0

代替。

我是MySQL的初学者,我在这一个请求中迷失了。

1 个答案:

答案 0 :(得分:0)

这应该适合你:

SELECT T.IdGroup as Group_Id,
       COUNT(T.IdGroup) as NB_TASK_TOTAL,
       (SELECT COUNT(Tt.Id) FROM table_task Tt WHERE Tt.IdGroup = T.IdGroup AND Tt.Status = 1) as NB_TASK_CREATED,
       (SELECT COUNT(Tt.Id) FROM table_task Tt WHERE Tt.IdGroup = T.IdGroup AND Tt.Status = 2 AND Tt.Id NOT IN (SELECT IdAfterTask from table_dependency as D1 join table_task as T1 on T1.Id=D1.IdBeforeTask WHERE T1.Status!=11)) as NB_TASK_UNSURE
  FROM table_task T
 GROUP BY T.IdGroup;