Problem
I have a PostgreSQL database on which I am trying to summarize the cash register revenue over time. The cash register may have the status ACTIVE or INACTIVE, but I only want to summarize the income generated when it was active for a certain period of time.
I have two tables; which marks the income and the one that marks the status of the cash register:
CREATE TABLE counters ( id bigserial NOT NULL, "timestamp" timestamp with time zone, total_revenue bigint, id_of_machine character varying(50), CONSTRAINT counters_pkey PRIMARY KEY (id) ) CREATE TABLE machine_lifecycle_events ( id bigserial NOT NULL, event_type character varying(50), "timestamp" timestamp with time zone, id_of_affected_machine character varying(50), CONSTRAINT machine_lifecycle_events_pkey PRIMARY KEY (id) )
A counter record is added every 1 minute, and total_revenue only increases. The machine_lifecycle_events entry is added every time a machine state changes.
I have added an image illustrating the problem. This is the revenue during the blue periods to be added up.

What I tried so far
I created a query that can give me the total revenue at the moment:
SELECT total_revenue FROM counters WHERE timestamp < '2014-03-05 11:00:00' AND id_of_machine='1' ORDER BY timestamp desc LIMIT 1
Questions
- How to calculate the income received between two timestamps?
- How do I determine the start and end timestamps of blue periods when I have to compare timestamps in machine_lifecycle_events with the input period?
Any ideas on how to attack this issue?
Update
Sample data:
INSERT INTO counters VALUES (1, '2014-03-01 00:00:00', 100, '1') , (2, '2014-03-01 12:00:00', 200, '1') , (3, '2014-03-02 00:00:00', 300, '1') , (4, '2014-03-02 12:00:00', 400, '1') , (5, '2014-03-03 00:00:00', 500, '1') , (6, '2014-03-03 12:00:00', 600, '1') , (7, '2014-03-04 00:00:00', 700, '1') , (8, '2014-03-04 12:00:00', 800, '1') , (9, '2014-03-05 00:00:00', 900, '1') , (10, '2014-03-05 12:00:00', 1000, '1') , (11, '2014-03-06 00:00:00', 1100, '1') , (12, '2014-03-06 12:00:00', 1200, '1') , (13, '2014-03-07 00:00:00', 1300, '1') , (14, '2014-03-07 12:00:00', 1400, '1'); INSERT INTO machine_lifecycle_events VALUES (1, 'ACTIVE', '2014-03-01 08:00:00', '1') , (2, 'INACTIVE', '2014-03-03 00:00:00', '1') , (3, 'ACTIVE', '2014-03-05 00:00:00', '1') , (4, 'INACTIVE', '2014-03-06 12:00:00', '1');
SQL Fiddle with sample data.
Request example:
The income between "2014-03-02 08:00:00" and "2014-03-06 08:00:00" is 300. 100 for the first ACTIVE period and 200 for the second ACTIVE period.