I will not answer you how to do this in the database, I will propose you a way that (in my opinion) is more suitable for calculated values. Where these calculated fields would not be fields saved in the database but rather calculated based on their indicators.
Upside:
- The field will be recalculated for each new request, based on the current state of the object;
- You do not need Procedures, Functions, or Triggers for this task linked to databases;
- You do not run the risk of the calculated field becoming outdated based on your indicators;
- Among other numerous minor advantages it is possible to point out easily;
Disadvantages:
- If the bank is used by more than one system, all those involved must incorporate the rule to calculate the calculated fields;
- If you do not use Classes template (Assigns the query cursor directly to Datasource (particularly not recommend));
Implementation
It would be something similar to this in C# (which seems to be the language you are 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 columns:
ID | DATA_CALIBRAGEM | VENCIMENTO_CALIBRAGEM
I understand Fernando. I will try to apply your example in the system. This way I think I can display these status in my listview. Because besides updating this status I need to display. I have never used classes, but I will try to apply in the code I already have.
– Philipe Said
@Philipesaid, I think it will suit your scenario. And I recommend layering your system and model classes, because using Query Cursor directly in the View may seem practical at first, but as the application grows and maintenance consequently grows as well, Not having layers and classes makes things much more difficult to control. But that’s more of a matter of opinion!
– Fernando Leal