Select intervals / first and last occurrence of column combinations from PostgreSQL -


-- have postgresql database table

create table timer (   id character varying(12),   date timestamp without time zone,   id1 character varying(10),   id2 character varying(10) ) (   oids=false ); alter table timer   owner postgres; 

-- , data

insert timer values ('001', '2015-01-01 12:00:00', 100, null); insert timer values ('002', '2015-01-01 12:00:00', 200, null); insert timer values ('003', '2015-01-01 12:00:10', 100, null); insert timer values ('004', '2015-01-01 12:00:10', 200, null); insert timer values ('005', '2015-01-01 12:00:20', 100, 'aaaa'); insert timer values ('006', '2015-01-01 12:00:20', 200, null); insert timer values ('007', '2015-01-01 12:00:30', 100, 'aaaa'); insert timer values ('008', '2015-01-01 12:00:30', 200, null); insert timer values ('009', '2015-01-01 12:00:40', 100, 'aaaa'); insert timer values ('010', '2015-01-01 12:00:40', 200, 'bbbb'); insert timer values ('011', '2015-01-01 12:00:50', 100, 'aaaa'); insert timer values ('012', '2015-01-01 12:00:50', 200, 'bbbb'); insert timer values ('013', '2015-01-01 12:01:00', 100, 'aaaa'); insert timer values ('014', '2015-01-01 12:01:00', 200, 'aaaa'); insert timer values ('015', '2015-01-01 12:01:10', 100, 'aaaa'); insert timer values ('016', '2015-01-01 12:01:10', 200, 'aaaa'); insert timer values ('017', '2015-01-01 12:01:20', 100, null); insert timer values ('018', '2015-01-01 12:01:20', 200, 'aaaa'); insert timer values ('019', '2015-01-01 12:01:30', 100, null); insert timer values ('020', '2015-01-01 12:01:30', 200, 'aaaa'); insert timer values ('021', '2015-01-01 12:01:40', 100, null); insert timer values ('022', '2015-01-01 12:01:40', 200, 'aaaa'); insert timer values ('023', '2015-01-01 12:01:50', 100, 'bbbb'); insert timer values ('024', '2015-01-01 12:01:50', 200, 'aaaa'); insert timer values ('025', '2015-01-01 12:02:00', 100, 'bbbb'); insert timer values ('026', '2015-01-01 12:02:00', 200, 'aaaa');  select * timer; id  |         date        |  id1 |   id2  | ----+---------------------+------+--- ----+ 001 | 2015-01-01 12:00:00 | 100  |  null  | 002 | 2015-01-01 12:00:00 | 200  |  null  | 003 | 2015-01-01 12:00:10 | 100  |  null  | 004 | 2015-01-01 12:00:10 | 200  |  null  | 005 | 2015-01-01 12:00:20 | 100  |  aaaa  | 006 | 2015-01-01 12:00:20 | 200  |  null  | 007 | 2015-01-01 12:00:30 | 100  |  aaaa  | 008 | 2015-01-01 12:00:30 | 200  |  null  | 009 | 2015-01-01 12:00:40 | 100  |  aaaa  | 010 | 2015-01-01 12:00:40 | 200  |  bbbb  | 011 | 2015-01-01 12:00:50 | 100  |  aaaa  | 012 | 2015-01-01 12:00:50 | 200  |  bbbb  | 013 | 2015-01-01 12:01:00 | 100  |  aaaa  | 014 | 2015-01-01 12:01:00 | 200  |  aaaa  | 015 | 2015-01-01 12:01:10 | 100  |  aaaa  | 016 | 2015-01-01 12:01:10 | 200  |  aaaa  | 017 | 2015-01-01 12:01:20 | 100  |  null  | 018 | 2015-01-01 12:01:20 | 200  |  aaaa  | 019 | 2015-01-01 12:01:30 | 100  |  null  | 020 | 2015-01-01 12:01:30 | 200  |  aaaa  | 021 | 2015-01-01 12:01:40 | 100  |  null  | 022 | 2015-01-01 12:01:40 | 200  |  aaaa  | 023 | 2015-01-01 12:01:50 | 100  |  bbbb  | 024 | 2015-01-01 12:01:50 | 200  |  aaaa  | 025 | 2015-01-01 12:02:00 | 100  |  bbbb  | 026 | 2015-01-01 12:02:00 | 200  |  aaaa  | 

every 10 seconds server receives new data 2 id-s. id1 , id2 have display result in format:

|     date_start      |      date_end       | id1 | id2  | +---------------------+---------------------+-----+------+ | 2015-01-01 12:00:00 | 2015-01-01 12:00:10 | 100 | null | | 2015-01-01 12:00:00 | 2015-01-01 12:00:30 | 200 | null | | 2015-01-01 12:00:20 | 2015-01-01 12:01:10 | 100 | aaaa | | 2015-01-01 12:00:40 | 2015-01-01 12:00:50 | 200 | bbbb | | 2015-01-01 12:01:10 | 2015-01-01 12:02:00 | 200 | aaaa | | 2015-01-01 12:01:20 | 2015-01-01 12:01:40 | 100 | null | | 2015-01-01 12:01:50 | 2015-01-01 12:02:00 | 100 | bbbb | 

result in format
first occurrence of combinations id1/id2 , last 1 before id2 changed.
example row id:001 starts values date:'2015-01-01 12:00:00' id1:100 id2:null
, first change row in row id:005 id2 becomes aaaa
possible in 1 query or should calculations periodically.

you may use simple max , min aggregate functions group by receive result need:

select min(date) date_start, max(date) date_end, id1, id2 timer group id1, id2 order date_start, id1 nulls first, id2 nulls first; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -