postgresql - to write a SQL query which select rows where column value changed from previous row -
create table status( id serial not null, site_id integer, sigplan smallint, server_time timestamp without time zone constraint status_data_pkey primary key (id)) (oids=false); alter table traffview.status_data owner postgres;
index: traffview.status_data_idx
create index status_data_idx on traffview.status_data using btree (server_time, site_id);
i have table this
id siteid sigplan server_time 1 8300 1 2011-01-01 2 8300 1 2011-01-02 3 8300 2 2011-01-03 4 9600 1 2011-01-04 5 9600 2 2011-01-05
how select rows sigplan changed previous row siteid?
in example above, query should return rows
2011-01-03 (sigplan changed 1 2 between 2011-01-01 , 2011-01-03 8300), 2011-01-05(sigplan changed 1 2 between 2011-01-04 , 2011-01-05 9600).
- the table contains lot of data query should optimized.
select siteid, sigplan, max(server_time) traffview.status_data group siteid, sigplan having count(1) > 1 , max(server_time) > 'xxxxx' , max(server_time) < 'xxxxx'
Comments
Post a Comment