Find ID's of "Handleplan" or "Handlinger" marked as ended on a specific customer and time interval
SELECT array_agg(params->'opts'->'0'->>'s_e_id' ORDER BY START) FROM [LOG_TABLE] WHERE customer_id=[CUSTOMER_ID] AND START > '[START_TIME]' AND START < '[END_TIME]' AND params::text LIKE '%afsluttet%' AND action = 'save_field';
SELECT et.name,fd.id,fd.alias,fd.name,f.entity_id,
CASE WHEN fd.alias='handleplan' THEN get_name((SELECT reference_id FROM FIELDS f2 JOIN field_descriptors fd2 ON (fd2.id=f2.field_descriptor_id) WHERE fd2.alias='person' AND f2.entity_id=f.reference_id LIMIT 1))
ELSE COALESCE(get_name(f.reference_id),f.value_text) END
FROM FIELDS f JOIN field_descriptors fd ON (fd.id=f.field_descriptor_id)
JOIN entities e ON (e.id=f.entity_id)
JOIN entity_types et ON (et.id=e.entity_type_id)
WHERE f.entity_id IN ([IDS_FROM_PREVIOUS_QUERY])
ORDER BY f.entity_id, fd.position;