Guest User

Generate flat table

a guest
Jul 16th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | Source Code | 0 0
  1. SELECT name,
  2. encounter_type_id,
  3. CONCAT('{',
  4. '"report_name": "', name, '", ',
  5. '"flat_table_name": "', table_name, '", ',
  6. '"encounter_type_uuid": "', uuid, '", ',
  7. '"table_columns": ', json_obj, ' ',
  8. '}') AS json_data,
  9. encounter_type_uuid
  10. FROM (SELECT DISTINCT et.name,
  11. encounter_type_id,
  12. concat('mamba_flat_encounter_',
  13. LOWER(LEFT(REGEXP_REPLACE(REGEXP_REPLACE(et.name, '[^a-zA-Z0-9_ ]+', '_'), '[ _]+', '_'),
  14. 18))) AS table_name,
  15. et.uuid,
  16. (SELECT JSON_OBJECTAGG(name, uuid)
  17. FROM (SELECT DISTINCT et.encounter_type_id,
  18. LOWER(LEFT(REGEXP_REPLACE(REGEXP_REPLACE(cn.name, '[^a-zA-Z0-9_ ]+', '_'),
  19. '[ _]+', '_'), 35)) AS name,
  20. c.uuid
  21. FROM openmrs.obs o
  22. INNER JOIN openmrs.encounter e
  23. ON e.encounter_id = o.encounter_id
  24. INNER JOIN openmrs.encounter_type et
  25. ON e.encounter_type = et.encounter_type_id
  26. INNER JOIN openmrs.concept_name cn
  27. ON cn.concept_id = o.concept_id
  28. INNER JOIN openmrs.concept c
  29. ON cn.concept_id = c.concept_id
  30. WHERE et.name = 'Care and Treatment Service Enrolment'
  31. AND cn.locale = 'en'
  32. AND cn.voided = 0
  33. AND cn.locale_preferred = 1
  34. AND et.retired = 0) json_obj) json_obj,
  35. et.uuid as encounter_type_uuid
  36. FROM openmrs.encounter_type et
  37. INNER JOIN openmrs.encounter e
  38. ON e.encounter_type = et.encounter_type_id
  39. WHERE et.name = 'Care and Treatment Service Enrolment') as final;
Advertisement
Add Comment
Please, Sign In to add comment