b-b-a/bba-power — Issue #107: Selecting the required contract when using a given MeterID
Author: epounce
Created: 2012-02-28 17:31:05+00:00
State: open
Labels: Design Issue

When joining tables from Meter to Meter_Contract and then on to Contract we need to make sure we are getting the Meter_Contract and Contract information we want.
There can be multiple Contracts for a Meter over time. Therefore, for each Meter there can be many Meter_Contracts with a Contract attached.
This will mean that looking up a Meter will usually (?) give a set of rows - one row for each contract on the meter.

The easy solution is to make sure that the set of rows is reduced to one with a where clause - using contract_status or the contract_dateStart etc.
[selecting the contract_dateStart field using "MAX(contract_dateStart)" is effective is just getting the latest contract.]

When Usage is also linked it can be important to make sure the appropriate contract is used for a given set of usage figures (they may be from a different supplier) - here the where clause will need to have the "usage_dateReading between the contract_dateStart and dateEnd".
(Although there is a problem here - the last reading date is often just after the end of contract!)