3

我有一个记录项目状态的表:

CREATE TABLE `project_states` (
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `state_id` INT(10) NULL DEFAULT NULL,
    `project_id` INT(10) NULL DEFAULT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB

一些假数据:

    id  | state_id | project_id | created_at
-----------------------------------------------------
     1  |   2      |   8        | 2014-05-27 10:58:12   
     2  |   3      |   8        | 2014-05-27 11:10:34
     3  |   8      |   8        | 2014-05-27 11:56:48
     4  |   2      |  10        | 2014-05-27 11:08:34
     5  |   4      |  10        | 2014-05-27 11:59:01

我正在尝试获取两个状态(例如 2 和 8)之间的时差,并且仅适用于具有两种状态的项目;在这种情况下,仅适用于项目 8(因为 10 没有状态 8)。

到目前为止,我很成功地选择了符合条件的项目(具有两个值,而不仅仅是一个),但是此查询为每个匹配的项目返回一个结果元组:

SELECT t.*
   FROM (
     SELECT ps.*
     FROM project_states ps
     WHERE ps.state_id IN (2,8) 
   ) as t
JOIN project_states pro ON pro.project_id = t.project_id
WHERE pro.state_id = 8

正确返回:

    id  | state_id | project_id | created_at
-----------------------------------------------------
     1  |   2      |   8        | 2014-05-27 10:58:12   
     3  |   8      |   8        | 2014-05-27 11:56:48

我很确定它可以工作,因为如果我将缺失的状态添加到另一个项目,它会返回新的结果元组:

    id  | state_id | project_id | created_at
-----------------------------------------------------
     1  |   2      |   8        | 2014-05-27 10:58:12   
     3  |   8      |   8        | 2014-05-27 11:56:48
     4  |   2      |  10        | 2014-05-27 11:08:34
     6  |   8      |  10        | 2014-05-27 12:03:08

但是我如何计算时差?我正在使用 PHP,我知道我可以通过 project_id 遍历结果,然后计算差异,但我认为可能有一个纯 SQL 解决方案会产生如下结果:

project_id | difference
------------------------
     8     | 0000-00-00 01:02:00
    10     | 0000-00-00 01:05:26

好吧,实际上我的目标是计算项目在这两个选定状态之间的平均时间差,因此所有记录都可以归结为一个平均值,但这可能是我稍后发现的问题。

4

4 回答 4

1

实际上,您不需要,因为您可以通过指定适当的子句JOIN来解决问题:GROUP BY

SELECT
  SEC_TO_TIME(AVG(times.atime)) AS avg_time
FROM
  (SELECT
    project_id,
    TIMEDIFF(MAX(created_at), MIN(created_at)) AS atime
  FROM
    project_states
  WHERE
    state_id IN (2,8)
  GROUP BY
    project_id
  HAVING
    COUNT(DISTINCT id)>1) as times

看来我迟到了,但至少,那是因为我做了小提琴

于 2014-05-27T13:43:13.760 回答
1

如果您只有 2 个状态,则可以使用简单的 INNER JOIN。和TIMESTAMPDIFF()以分钟为单位获得差异,例如:

SELECT p.project_id,
       TIMESTAMPDIFF(MINUTE,p.created_at,p1.created_at) 
          as state_minutes_diff

FROM project_states as p
JOIN project_states as p1 
  ON p.project_id=p1.project_id
     AND p1.state_id=8  

WHERE p.state_id=3

SQLFiddle demo

要获得所有项目的平均值:

SELECT AVG(TIMESTAMPDIFF(MINUTE,p.created_at,p1.created_at))
          as AVG_state_minutes_diff
FROM project_states as p
JOIN project_states as p1 
  ON p.project_id=p1.project_id
     AND p1.state_id=8  
WHERE p.state_id=3

SQLFiddle demo

于 2014-05-27T13:42:17.237 回答
1

我会使用聚合和有子句来做到这一点:

select ps.project_id,
       timestampdiff(second, min(case when ps.state_id = 2 then created_at end),
                     max(case when ps.state_id = 8 then created_at)
                    ) as diff_in_secs

from project_states ps
where ps.state_id in (2, 8)
group by ps.project_id
having count(distinct ps.state_id) = 2;

您也可以使用连接来执行此操作:

select ps2.project_id, timestampdiff(second, ps2.created_at, ps8.created_at)
from project_states ps2 join
     project_states ps8
     on ps2.project_id = ps8.project_id and
        ps2.state_id = 2 and ps8.state_id = 8;
于 2014-05-27T13:39:03.150 回答
0
SELECT project_id,
       timediff(min(created_at), max(created_at)) as difference
FROM project_states
WHERE state_id IN (2,8) 
GROUP BY project_id 
having count(distinct state_id) = 2
于 2014-05-27T13:37:47.013 回答