2013-08-05 47 views
0

我有2 tbls如下。如何在MySQL中使用2個表編寫計算查詢?

請求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年8月6日) - (2013年8月5日)=1天(2013-08-07) - (2013-08-06)= 1天

實施時間 =(落實compeleted) - (審查對 實施)= 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的同:http://www.sqlfiddle.com/#!2/fc6db/1

0

不知道我是否理解你需要什麼,但這裏是我從你的問題中脫穎而出的例子。在示例中,我使用嵌套查詢來獲取每個請求的計算結果。 DATEDIFF以天爲單位給出了兩個日期的差異。我使用ABS,因爲我假設你不想要負數,我不確定那些日期總是給出正數。那些在例子中給它沒有abs allsoso。

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 

Fiddle