I am trying for several days to solve the following problem: let's say we have
table users
user_id, credit
table properties
property_id, user_id, check_interval
tables states_(year) (i.e. multiple tables - for each year one starting 2010 - i.e. states_2010 - when the application was born)
property_id, timestamp
The logic behind is that each user has one or more properties. Each property has one or more states. Each state has been obtained as of the timestamp. States are obtained once per certain time, which is check_interval (for each property specific). Obtaining each state costs each user one credit which is deducted from the user account (table users).
My aim is to get an overview with property_ids and time of next check (each property_id only once) starting from the checks planned for the immediate future. Time of the next check is the timestamp of the last state (i.e. max(timestamp) from all of the states_(year) tables) plus check_interval. That overview should contain only property_id of properties belonging to users that have some credit (i.e. credit > 0).
I have found very close question (Get max row per group from a related table) but it only relates to three tables and is solving my issue only partially. In my case, there is also complication with users' credit and multiple states_(year) tables which is something I cannot break.