Ingres SQL,根据另一列的值查找一列的最大值

时间:2014-06-07 20:28:05

标签: sql aggregate-functions ingres

我正在使用我从其他人继承的脚本处理Ingres数据库。我需要更改脚本以取出最新的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'

3 个答案:

答案 0 :(得分:1)

我会使用子选择。试试: -

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)

答案 1 :(得分: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()函数,我会为您解释。

P.S:table1中的主键在哪里?!!

答案 2 :(得分: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');