I'm working on an Ingres DB with a script I've inherited from someone else. I need to change the script to pull out the action_times of the latest start_time and end_time event, and also the difference between the two. A sample of the DB is listed below
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
So far what I've come up with is:
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'
but the results are coming out as follows:
BIG SMALL DIFF
----------------------------------------------------------------------------
2014-05-27 10:10:11 2014-05-27 10:10:57 null
Apologies if a question like this has already been answered (I'm sure it probably has) but I've spent a couple of days looking over various forums and I can't find a similar example, probably how I'm phrasing the search terms. Any help would be much appreciated, I'm pretty sure I would have covered something like this in college but that was a few years ago and my SQL is a bit rusty these days. Thanks in advance!
Edit: So after some research I have come up with the following which will work in the 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'