Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Base query
- ransack_query = Interaction.ransack(interactions_event_query).result
- emp_ids = ransack_query.pluck(Arel.sql("(metadata->>'employee_id')::INTEGER")).uniq
- # employee_searches is a materialized view and it doesn't have an employee_id column which can be used as a default in the join, Also the number of rows can be large
- employees = "SELECT distinct id, #{compare_key} FROM employee_searches where id in (#{emp_ids.join(',')})"
- # interactions table has a JSONB column named metadata which has a key named employee_id
- total_interactions = ransack_query
- .select("interaction_type, (metadata->>'employee_id')::INTEGER as employee_id")
- .joins("LEFT JOIN (#{employees}) AS employees ON interactions.employee_id = employees.id")
- .group("interaction_type", "employees.#{compare_key}")
- .count
- unique_interactions = ransack_query
- .select("distinct interaction_type, (metadata->>'employee_id')::INTEGER as employee_id")
- .joins("LEFT JOIN (#{employees}) AS employees ON interactions.employee_id = employees.id")
- .group("interaction_type", "employees.#{compare_key}")
- .count
Advertisement
Add Comment
Please, Sign In to add comment