Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. with pharmscript_facilities AS (
  2. select id from facility where facility_category_id in (select id from facility_category where name = 'pharmscript')
  3. )
  4.  
  5. update transaction set eligible_as_of = now()
  6. from new_distinct_transactions
  7. where new_distinct_transactions.id = transaction.id AND
  8. not exists (select * from transaction t
  9. left outer join recommendation_rule r on t.recommendation_rule_id = r.id
  10. where t.id != new_distinct_transactions.id AND
  11. t.facility_id = new_distinct_transactions.facility_id AND
  12. t.resident_name = new_distinct_transactions.resident_name AND
  13. t.eligible_as_of is not null AND
  14. t.inactive_as_of is null AND
  15. (t.disregard is not true OR t.show_disregard is not true OR t.deleted is not true) AND
  16. (t.dispense_date < new_distinct_transactions.dispense_date OR (t.dispense_date = new_distinct_transactions.dispense_date AND t.id < new_distinct_transactions.id)) AND
  17.  
  18. NOT (
  19. exists (select * from recommendation_rule where show_x_days_before_depleted is true AND t.recommendation_rule_id = recommendation_rule.id) AND
  20. t.status not in ('Resident Refused Change', 'MD Declined Change')
  21. ) AND
  22.  
  23. (
  24. t.drug_name = new_distinct_transactions.drug_name OR
  25. (
  26. new_distinct_transactions.no_previous_orders_of is not null AND
  27. new_distinct_transactions.no_previous_orders_of != '' AND
  28. t.drug_name ilike '%' || new_distinct_transactions.no_previous_orders_of || '%'
  29. )
  30. )
  31. AND
  32. (t.facility_id not in pharmscript_facilities)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement