0

我有 2 tbls 如下。

请求表

id | request | created
1  | asdf    | 2013-07-04 14:39:03
2  | qwer    | 2013-07-10 12:06:37

历史表

id | request_id | status       | date
1  |     1      | Pending      | 2013-07-04 14:39:03
2  |     1      | Reviewing    | 2013-07-05 01:10:14
3  |     1      | Implementing | 2013-07-06 11:25:54
4  |     1      | Completed    | 2013-07-07 12:36:32
5  |     2      | Pending      | 2013-07-10 15:05:56
6  |     2      | Reviewing    | 2013-07-11 03:08:04
7  |     2      | Implementing | 2013-07-13 11:45:48
8  |     2      | Completed    | 2013-07-17 14:28:15

我想在 2 tbls 以上显示如下

Request | Reviewing Time | Implementing Time 
asdf    |      0         |      0            
qwer    |      1         |      2

以 request_id = 1 为例的理论是

审查到实施 = (2013-08-06) - (2013-08-05) = 1 天

待审核 = (2013-08-05) - (2013-08-04) = 1 天

审核时间=(审核到实施)-(待审核)= 0天

审查到实施 = (2013-08-06) - (2013-08-05) = 1 天

实施至完成 = (2013-08-07) - (2013-08-06) = 1 天

实施时间=(实施到完成)-(审查到实施)= 0

4

2 回答 2

1

这是解决它的长期方法......

 CREATE TABLE my_table 
 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,request_id INT NOT NULL
 ,status       VARCHAR(20) NOT NULL
 ,date DATETIME NOT NULL
 ,UNIQUE(request_id,status)
 );

 INSERT INTO my_table VALUES
 (1  ,1      ,'Pending','2013-07-04 14:39:03'),
 (2  ,     1      ,'Reviewing','2013-07-05 01:10:14'),
 (3  ,     1      ,'Implementing','2013-07-06 11:25:54'),
 (4  ,     1     ,'Completed','2013-07-07 12:36:32'),
 (5  ,     2     ,'Pending','2013-07-10 15:05:56'),
 (6  ,     2     ,'Reviewing','2013-07-11 03:08:04'),
 (7  ,     2     ,'Implementing','2013-07-13 11:45:48'),
 (8  ,     2     ,'Completed','2013-07-17 14:28:15');

 SELECT request_id
      , DATEDIFF(implementing,reviewing) - DATEDIFF(reviewing,pending) rT
      , DATEDIFF(completed,implementing) - DATEDIFF(implementing,reviewing) iT
 FROM (
 SELECT x.request_id
      , MAX(CASE WHEN status = 'pending' THEN date END) pending 
      , MAX(CASE WHEN status = 'reviewing' THEN date END) reviewing
      , MAX(CASE WHEN status = 'implementing' THEN date END) implementing
      , MAX(CASE WHEN status = 'completed' THEN date END) completed
   FROM my_table x
  GROUP 
     BY request_id
     ) a;

 +------------+------+------+
 | request_id | rT   | iT   |
 +------------+------+------+
 |          1 |    0 |    0 |
 |          2 |    1 |    2 |
 +------------+------+------+

相同的 sqlfiddle:http ://www.sqlfiddle.com/#!2/fc6db/1

于 2013-08-05T10:17:31.283 回答
0

不确定我是否正确理解了您的需求,但这是我从您的问题中读出的示例。在示例中,我使用嵌套查询来获取每个请求的计算。DATEDIFF 以天为单位给出 2 个日期的差异。我使用 ABS 是因为我假设您不想要负数,而且我不确定这些日期是否总是给出正数。示例中的那些也没有绝对值。

SELECT calc.request, ABS(calc.`reviewing to implementing`-calc.`pending to reviewing`) AS 'Reviewing Time', ABS(calc.`implementing to completed`-calc.`reviewing to implementing`) AS 'Implementing Time'
    FROM (
        SELECT t1.request,
        (
            SELECT ABS(DATEDIFF((SELECT `date` FROM history_tbl WHERE request_id = t1.id AND `status` = 'Reviewing'), (SELECT `date` FROM history_tbl WHERE request_id = t1.id AND `status` = 'Implementing')))
        ) AS 'reviewing to implementing',
        (
            SELECT ABS(DATEDIFF((SELECT `date` FROM history_tbl WHERE request_id = t1.id AND `status` = 'Pending'), (SELECT `date` FROM history_tbl WHERE request_id = t1.id AND `status` = 'Reviewing')))
        ) AS 'pending to reviewing',
        (
            SELECT ABS(DATEDIFF((SELECT `date` FROM history_tbl WHERE request_id = t1.id AND `status` = 'Implementing'), (SELECT `date` FROM history_tbl WHERE request_id = t1.id AND `status` = 'Completed')))
        ) AS 'implementing to completed'
        FROM request_tbl as t1
    ) AS calc

小提琴

于 2013-08-05T10:21:57.593 回答