I will not answer you how to do this in the Database, I will propose a way that (in my opinion) is more appropriate for calculated values. Where these calculated fields would not be fields saved in the database but calculated based on their indicators.
Advantage:
- The field will be recalculated at each new request, based on the current state of the object;
- You do not need Procedures, Functions, or Triggers for this task linked to the database;
- You do not run the risk of the calculated field being out of date based on your indicators;
- Among other numerous small advantages that you can easily point out;
Disadvantages:
- If the bank is used by more than one system, all those involved will have to enter the rule to calculate the calculated fields;
- If you do not use a Class template (Assign the query cursor directly to the DataSource (particularly I do not recommend));
Implementation
It would be something similar to this in C # (which seems to be the language you're using):
public class Calibragem{
public long Id { get; set; }
public DateTime Data { get; set; }
public DateTime Vencimento { get; set; }
public StatusCalibragem Status {
get{
// aqui você faz a conta da diferença em mémoria
/**
* DUVIDA: aqui você não deveria calcula a Data da ultima calibragem com a
* data atual(DateTime.Now) ou o Vencimento com a data atual(DateTime.Now),
* ao invés de fazer o calculo entre as duas Datas?
**/
TimeSpan diff = Data - Vencimento;
// verifica a diferença em dias para ver qual o status atual
if(diff.Days > 30){
return StatusCalibragem.EmDia;
}
if(diff.Days < 30){
return StatusCalibragem.Calibrar;
}
// não entendi quando será Vencida, com sua explicação
}
private set;
}
}
public enum StatusCalibragem
{
EmDia,
Calibrar,
Vencida
}
In the database your table would only have the columns:
ID | DATA_CALIBRAGEM | VENCIMENTO_CALIBRAGEM