我有2个tbls如下。
请求tbl
id | request | created
1 | asdf | 2013-07-04 14:39:03
2 | qwer | 2013-07-10 12:06:37
历史tbl
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
答案 0 :(得分: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 of same:http://www.sqlfiddle.com/#!2/fc6db/1
答案 1 :(得分:0)
不确定我是否理解你需要什么,但这里是我从你的问题中得到的例子。在示例中,我使用嵌套查询来获取每个请求的计算。 DATEDIFF以天为单位给出2个日期的差异。我使用ABS,因为我假设你不想要负数,我不确定这些日期总是给出正数。示例中的那些没有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