[英]How to write a calculation query in MySQL with 2 tables?
我有2湯匙如下。
請求tbl
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
這是解決問題的長途之路...
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 |
+------------+------+------+
不確定我是否理解您的需求,但這是我從您的問題中讀出的內容的示例。 在示例中,我使用嵌套查詢來獲取每個請求的計算結果。 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.