I have a table of events (with id used as event id) like below (see also SQL fiddle here):
CREATE TABLE ext (
key INT,
id CHAR(1),
pid INT,
sid INT,
oid INT,
event VARCHAR(3)
);
INSERT INTO ext (key, id, pid, sid, oid, event)
VALUES
(1, 'Q', 1, 81, 20, 'tsu'),
(2, 'Q', 1, 81, 9, 'tsu'),
(3, 'Q', 1, 81, 10, 'tsu'),
(4, 'Q', 1, 81, 4, 'tsu'),
(5, 'Q', 1, 81, 15, 'tsu'),
(6, 'Q', 1, 81, 3, 'tsu'),
(7, 'Q', 1, 81, 5, 'tsu'),
(8, 'Q', 1, 81, 18, 'tsu'),
(9, 'Q', 1, 81, 2, 'tsu'),
(10, 'Q', 1, 81, 1, 'tsu'),
(11, 'Q', 1, 81, 7, 'tsu'),
(12, 'f', 2, 2, NULL, 's'),
(13, 'Z', 2, 871, NULL, 'e'),
(14, 'm', 3, 872, 2, 'pof'),
(15, 's', 3, 873, 31, 'pom'),
(16, 'R', 3, 874, 15, 'fc'),
(17, 'R', 3, 874, 1, 'fc'),
(18, 'R', 3, 874, 31, 'fc'),
(19, 'R', 3, 874, 9, 'fc'),
(20, 'R', 3, 874, 10, 'fc'),
(21, 'R', 3, 874, 4, 'fc'),
(22, 'R', 3, 874, 7, 'fc'),
(23, 'R', 3, 874, 3, 'fc'),
(24, 'R', 3, 874, 5, 'fc'),
(25, 'R', 3, 874, 18, 'fc'),
(26, 'R', 3, 874, 20, 'fc'),
(27, 'k', 3, 876, NULL, 's'),
(28, 'a', 3, 950, 31, 'rco'),
(29, 'y', 3, 1285, 7, 'pof'),
(30, 'N', 3, 1286, 22, 'pom'),
(31, 'i', 3, 1299, 1, 'fc'),
(32, 'i', 3, 1299, 5, 'fc'),
(33, 'i', 3, 1299, 3, 'fc'),
(34, 'i', 3, 1299, 20, 'fc'),
(35, 'i', 3, 1299, 4, 'fc'),
(36, 'i', 3, 1299, 9, 'fc'),
(37, 'i', 3, 1299, 10, 'fc'),
(38, 'i', 3, 1299, 22, 'fc'),
(39, 'i', 3, 1299, 15, 'fc'),
(40, 'i', 3, 1299, 18, 'fc'),
(41, 'I', 3, 1407, 9, 'pof'),
(42, 'T', 3, 1408, 19, 'pom'),
(43, 'u', 3, 1575, 4, 'pof'),
(44, 'V', 3, 1576, 30, 'pom'),
(45, 'B', 3, 2019, NULL, 'e'),
(46, 'h', 4, 60, NULL, 'e');
The events are ordered according to pid and sid columns (you can view them, for example, as days and hours respectively in the sense that pid is bigger time unit than sid so you should order first by pid and then by sid to obtain correct order). As you can see, some events (event = tsu or event = fc) have more than one row as they refer to many objects (oid), some refer to only one object (as the event of id = m), and some have only one row, but they actually refer to all previously observed object which are not 'dead'. They are some additional rules:
- an object is dead when they hit the
poforrcoevent (as stated ineventcolumn) - when the object hits the
pofevent, there is another object that replaces it marked withpomevent
I need to keep track of currently active objects. Therefore I'd like to "cross join" the rows where oid is NULL with all the active objects that can be deduced from the previous events, where by "cross join" I mean to replicate the row with oid = NULL for every currently active oid.
As the logic may be hard to grasp from the text, I have prepared the expected output (available on SQL fiddle here):
CREATE TABLE intermediate_result (
id CHAR(1),
pid INT,
sid INT,
oid INT,
event VARCHAR(3)
);
INSERT INTO intermediate_result (id, pid, sid, oid, event)
VALUES
('Q', 1, 81, 20, 'tsu'),
('Q', 1, 81, 9, 'tsu'),
('Q', 1, 81, 10, 'tsu'),
('Q', 1, 81, 4, 'tsu'),
('Q', 1, 81, 15, 'tsu'),
('Q', 1, 81, 3, 'tsu'),
('Q', 1, 81, 5, 'tsu'),
('Q', 1, 81, 18, 'tsu'),
('Q', 1, 81, 2, 'tsu'),
('Q', 1, 81, 1, 'tsu'),
('Q', 1, 81, 7, 'tsu'),
('f', 2, 2, 20, 's'),
('f', 2, 2, 9, 's'),
('f', 2, 2, 10, 's'),
('f', 2, 2, 4, 's'),
('f', 2, 2, 15, 's'),
('f', 2, 2, 3, 's'),
('f', 2, 2, 5, 's'),
('f', 2, 2, 18, 's'),
('f', 2, 2, 2, 's'),
('f', 2, 2, 1, 's'),
('f', 2, 2, 7, 's'),
('Z', 2, 871, 20, 'e'),
('Z', 2, 871, 9, 'e'),
('Z', 2, 871, 10, 'e'),
('Z', 2, 871, 4, 'e'),
('Z', 2, 871, 15, 'e'),
('Z', 2, 871, 3, 'e'),
('Z', 2, 871, 5, 'e'),
('Z', 2, 871, 18, 'e'),
('Z', 2, 871, 2, 'e'),
('Z', 2, 871, 1, 'e'),
('Z', 2, 871, 7, 'e'),
('m', 3, 872, 2, 'pof'),
('s', 3, 873, 31, 'pom'),
('R', 3, 874, 15, 'fc'),
('R', 3, 874, 1, 'fc'),
('R', 3, 874, 31, 'fc'),
('R', 3, 874, 9, 'fc'),
('R', 3, 874, 10, 'fc'),
('R', 3, 874, 4, 'fc'),
('R', 3, 874, 7, 'fc'),
('R', 3, 874, 3, 'fc'),
('R', 3, 874, 5, 'fc'),
('R', 3, 874, 18, 'fc'),
('R', 3, 874, 20, 'fc'),
('k', 3, 876, 15, 's'),
('k', 3, 876, 1, 's'),
('k', 3, 876, 31, 's'),
('k', 3, 876, 9, 's'),
('k', 3, 876, 10, 's'),
('k', 3, 876, 4, 's'),
('k', 3, 876, 7, 's'),
('k', 3, 876, 3, 's'),
('k', 3, 876, 5, 's'),
('k', 3, 876, 18, 's'),
('k', 3, 876, 20, 's'),
('a', 3, 950, 31, 'rco'),
('y', 3, 1285, 7, 'pof'),
('N', 3, 1286, 22, 'pom'),
('i', 3, 1299, 1, 'fc'),
('i', 3, 1299, 5, 'fc'),
('i', 3, 1299, 3, 'fc'),
('i', 3, 1299, 20, 'fc'),
('i', 3, 1299, 4, 'fc'),
('i', 3, 1299, 9, 'fc'),
('i', 3, 1299, 10, 'fc'),
('i', 3, 1299, 22, 'fc'),
('i', 3, 1299, 15, 'fc'),
('i', 3, 1299, 18, 'fc'),
('I', 3, 1407, 9, 'pof'),
('T', 3, 1408, 19, 'pom'),
('u', 3, 1575, 4, 'pof'),
('V', 3, 1576, 30, 'pom'),
('B', 3, 2019, 1, 'e'),
('B', 3, 2019, 5, 'e'),
('B', 3, 2019, 3, 'e'),
('B', 3, 2019, 20, 'e'),
('B', 3, 2019, 30, 'e'),
('B', 3, 2019, 19, 'e'),
('B', 3, 2019, 10, 'e'),
('B', 3, 2019, 22, 'e'),
('B', 3, 2019, 15, 'e'),
('B', 3, 2019, 18, 'e'),
('h', 4, 60, 1, 'e'),
('h', 4, 60, 5, 'e'),
('h', 4, 60, 3, 'e'),
('h', 4, 60, 20, 'e'),
('h', 4, 60, 30, 'e'),
('h', 4, 60, 19, 'e'),
('h', 4, 60, 10, 'e'),
('h', 4, 60, 22, 'e'),
('h', 4, 60, 15, 'e'),
('h', 4, 60, 18, 'e');
The SQL version is PostgreSQL 9.5.