Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. with odczyty2 as (
  2. with odczyty as (
  3. select
  4.     dev.code as dev_code,
  5.     coalesce(mcd.value_as_double, mcd.value_as_long) as value_as_number,
  6.     mcd.container_id as con_id,
  7.     mcd."timestamp",
  8.     us.email
  9. from devices dev
  10. left join device_parameters dev_par on dev_par.device_id = dev.id
  11. left join measuring_containers mc on mc.device_parameter_id = dev_par.id
  12. left join measuring_containers_data mcd on mcd.container_id = mc.id
  13. left join "identity".users us on us.id = dev.owner_id
  14. where
  15.     dev.owner_id = 65
  16. and
  17.     dev_par.parameter_id = 24
  18. and
  19.     mcd."timestamp" < '2019-06-19'
  20. and
  21.     mcd."timestamp" > '2019-06-17'
  22. --order by mcd."timestamp"
  23. )
  24. select
  25.     lead (value_as_number, -1)
  26.     over (partition by odczyty.dev_code) as prev_value
  27.     ,
  28.     odczyty.dev_code,
  29.     odczyty.con_id,
  30.     odczyty.value_as_number,
  31.     odczyty."timestamp",
  32.     odczyty.email
  33. from odczyty
  34. )
  35. select
  36.     odczyty2.dev_code,
  37.     odczyty2.con_id,
  38.     odczyty2.prev_value,
  39.     odczyty2.value_as_number,
  40.     odczyty2.value_as_number - odczyty2.prev_value as diff,
  41.     odczyty2."timestamp",
  42.     odczyty2.email
  43. from odczyty2
  44. where
  45.     odczyty2.value_as_number - odczyty2.prev_value < 0
  46. and
  47.     odczyty2.value_as_number - odczyty2.prev_value <= -0.001
  48. and
  49.     odczyty2.dev_code like '%water%'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement