Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object_id: int
- param_id: int
- date: date
- value: real
- param1_value as of two days ago > param2_value as of week low
- and sustained within past week
- select opvl_exist.object_id from obj_param_value_log opvl_exist
- where (select count(distinct opvl_sust1.date) from obj_param_value_log opvl_sust1 /*count dates from inner join to account for possible gaps in values for either parameter*/
- inner join obj_param_value_log opvl_sust2 on opvl_sust1.object_id=opvl_sust2.object_id
- where opvl_sust1.param_id=2 /*define param1*/
- and opvl_sust2.param_id=32 /*define param2*/
- and opvl_sust1.date>=current_date() - integer '6' /*limit lookup period for param1 to a week*/
- and opvl_sust2.date>=current_date() - integer '6' /*limit lookup period for param2 to a week*/
- and opvl_sust1.object_id=opvl_exist.object_id) =
- (select count(distinct opvl1.date) from obj_param_value_log opvl1 /*count dates where filter condition is true*/
- inner join obj_param_value_log opvl2 on opvl1.object_id=opvl2.object_id
- where opvl1.param_id=2 /*define param1*/
- and opvl2.param_id=32 /*define param2*/
- and opvl1.date between date current_date() - integer '2' - integer '6' and current_date() - integer '2' /*set a lookup period for param1 from a week and 2 days ago to just 2 days ago*/
- and opvl2.value=(select min(opvl_sq.value) from obj_param_value_log opvl_sq
- where opvl2.object_id=opvl_sq.object_id
- and opvl_sq.param_id=32
- and opvl_sq.date between opvl1.date - integer '4' and opvl1.date + integer '2') /*set a relative lookup period to calc the min value for param2 form 4 days before param1 date to 2 days ahead param1 date*/
- and opvl2.date>=current_date() - integer '6' - integer '6' /*set a lookup period for param2 from today to a week ago (min value lookup) and another week ago (condition sustained period)*/
- and opvl1.value>opvl2.value /*set condition param1 value > param2 value*/
- and opvl_exist.object_id=opvl1.object_id)
Add Comment
Please, Sign In to add comment