I have been asked to compile a SQL query for the following case:
ID | PARAMETER | VALID | VALUE
------------------------------
1 | Event1_Val | 1 | 0
1 | Event2_Val | 0 | 50.00
1 | Event2_Avg | 0 | 55.00
1 | Event1_Avg | 0 | 66.67
2 | Event1_Val | 0 | 37.50
2 | Event2_Val | 1 | 0
2 | Event1_Avg | 0 | 50.00
2 | Event2_Avg | 0 | 66.67
where _Val
is a current value. _Avg
is the average of the past five values.
Requirement 1: If VALID == 1
(applies only to _Val
values), do not check that parameter's _Val
and _Avg
, but check the other parameter's _Val
and _Avg
. In the example above for ID=1
, Event2_Val
and Event2_Avg
would be checked because Event1_Val.VALID = 1
(which means Event1_Val
and Event1_Avg
would not be checked.)
Requirement 2: Check the values of _Val
and _Avg
as a result of Requirement 1 for the following conditions:
_Val
< 5 OR_Val
+ 10 <_Avg
The conditions above return an alert. In the example above for ID=2
, Event1_Val
will return an alert because 37.50 + 10 is less than 50.00.
Requirement 3: Indicate the results of Requirement 2
ID | PARAMETER | ALERT
------------------------
1 | Event2_Val | 0
2 | Event1_Val | 1
I have tried to check for VALID
does not equal 1, but that leaves me with the following:
ID | PARAMETER | VALID | VALUE
------------------------------
1 | Event2_Val | 0 | 50.00
1 | Event2_Avg | 0 | 55.00
1 | Event1_Avg | 0 | 66.67
2 | Event1_Val | 0 | 37.50
2 | Event1_Avg | 0 | 50.00
2 | Event2_Avg | 0 | 66.67
I do not want to check for the parameter for the event in which _Val.VALID == 1
. Meaning, for ID=1
, I do not want to include Event1_Avg
.
Is there a way to group and check conditions using a query for this case? NOTE: There is a strong desire not to change values in the tables.
Try: