Using Postgres 9.5, I have a table properties:
CREATE TABLE properties (
id serial PRIMARY KEY,
property_id integer,
state character(2),
record_type character(1),
...
);
idis my unique internal id.property_idis from a 3rd party. Properties from different states may share the sameproperty_idbut there is only oneproperty_idper state. Reason being, thepropertiestable contains all states together instead of one state per table and theproperty_idcounter starts from 1 for each state.stateis the US state abbreviation (e.g. MA, CA, NY). When concatenated withproperty_idit references one property, e.g.12345NY.record_typecan beA(add),C(change), orD(delete).
When new properties are added to the table their record_type is A. Over time a properties' details change and there are new rows added to the table with C as their record_type.
Example:
id, property_id, state, record_type, ...
7353, 6001, 'MA', 'A', ...
7354, 6001, 'MA', 'C', ...
7355, 6001, 'MA', 'C', ...
Here's the problem: I want to only keep the most recent row for the property (doesn't matter what record_type) and delete all the older ones. So in the example, just keep the last row. There's no date column but we can assume the higher the id, the newer the record. As a side note, all the rows with D record types have been previously removed so we're only dealing with add and change record types.
record_type(andstate) is relevant for the question. Do you just want to keep one record perproperty_id, the rest of the field being irrelevant?