1

我正在使用从其他人那里继承的脚本来开发 Ingres DB。我需要更改脚本以提取最新的 start_time 和 end_time 事件的 action_times,以及两者之间的差异。下面列出了 DB 的示例

id_num  | version  | action_id  |   action_time
----------------------------------------------------------------------------
1         2          start_time     2014-05-26 14:58:14
1         2          end_time       2014-05-26 14:58:16
1         4          start_time     2014-05-27 10:10:57
1         4          end_time       2014-05-27 10:10:11

到目前为止,我想出的是:

SELECT max(a.action_time) as BIG, max(b.action_time) as SMALL, max(a.action_time) - max(b.action_time) as DIFF
FROM table1 as a, table1 as b,
WHERE a.id_num = '1' AND a.action_id = 'end_time' AND b.id_num = '1' AND b.action_id = 'start_time'

但结果如下:

BIG                         SMALL                   DIFF
----------------------------------------------------------------------------
2014-05-27 10:10:11         2014-05-27 10:10:57     null    

如果已经回答了这样的问题(我相信它可能已经回答了),我深表歉意,但我花了几天时间浏览各种论坛,但找不到类似的例子,可能是我对搜索的措辞条款。任何帮助将不胜感激,我很确定我会在大学里介绍过类似的东西,但那是几年前的事了,这些天我的 SQL 有点生疏了。提前致谢!

编辑:所以经过一些研究,我想出了以下将在 DB GUI 中工作的内容:

SELECT ingresdate(varchar(max(a.action_time))) as BIG, ingresdate(varchar(max(b.action_time))) as SMALL, date_part('secs',ingresdate(varchar(max(a.action_time))) - ingresdate(varchar(max(b.action_time)))) as DIFF
FROM table1 as a, table1 as b,
WHERE a.id_num = '1' AND a.action_id = 'end_time' AND b.id_num = '1' AND b.action_id = 'start_time'
4

3 回答 3

0

我会为此使用子选择。尝试 :-

select a.action_time as max_end_time, b.action_time as max_start_time, 
a.action_time - b.action_time as diff
from table a, table b

where a.action_time = (select max(action_time)
from table where action_id = 'end_time')

and b.action_time = (select max(action_time)
from table where action_id = 'start_time)
于 2014-07-24T20:48:34.213 回答
0

如果要计算 max(a.acction_time) 和 max(b.acction_time) 之间的差异,应使用以下脚本:

SELECT max(a.acction_time) as BIG, max(b.acction_time) as SMALL,DATEDIFF(s, max(a.acction_time), max(b.acction_time)) as DIFF
FROM table1 as a, table1 as b
WHERE a.id_num = '1' AND a.action_id = 'end_time' AND b.id_num = '1' AND b.action_id = 'start_time'

如果您不记得 DATEDIFF() 函数,我会为您解释。

PS:你的 table1 中的主键在哪里?!

于 2014-06-08T01:15:44.230 回答
0

这是我的尝试:

SELECT start.action_time, end.action_time, 
  interval('seconds', end.action_time - start.action_time ) as diff_secs
FROM
(
SELECT action_time
FROM table a
INNER JOIN 
(  SELECT max(id_num) as max_id_num, max(version) as max_version FROM table 
) b on ( id_num = max_id_num and version = max_version )
WHERE a.action_id = 'start_time'
) start
CROSS JOIN
(
SELECT action_time
FROM table a
INNER JOIN 
(  SELECT max(id_num) as max_id_num, max(version) as max_version FROM table 
) b on ( id_num = max_id_num and version = max_version )
WHERE a.action_id = 'end_time'
) end

使用您的数据,我得到以下输出:

+----------------------+----------------------+-----------+
|     action_time      |     action_time      | diff_secs |
+----------------------+----------------------+-----------+
| 27-May-2014 10:10:57 | 27-May-2014 10:10:11 |       -46 |
+----------------------+----------------------+-----------+

作为参考,这是我用来创建和填充测试表的脚本

CREATE TABLE table
(
id_num integer,
version integer,
action_id char(10),
action_time timestamp
)

INSERT INTO table VALUES (1,2,'start_time', '2014-05-26 14:58:14');
INSERT INTO table VALUES (1,2,'end_time', '2014-05-26 14:58:16');
INSERT INTO table VALUES (1,4,'start_time', '2014-05-27 10:10:57');
INSERT INTO table VALUES (1,4,'end_time', '2014-05-27 10:10:11');
于 2014-08-04T15:28:24.987 回答