SHOW FULL PROCESSLIST;


SELECT employee_registration_id, sa.`document_identity`, special_allowance_type_id, allowance_for, special_allowance_category_id, sad.amount, sad.approved_amount
,(
SELECT allowance_value
FROM `employee_allowance_policy`
WHERE employee_registration_id = sa.employee_registration_id
AND allowance_type_id = sa.special_allowance_type_id
AND relation=(CASE WHEN sad.allowance_for='Child' THEN 'Children' ELSE sad.allowance_for END)
AND allowance_category_id = sad.special_allowance_category_id
LIMIT 1
) AS percent_value
,(
SELECT allowance_value
FROM `employee_allowance_policy`
WHERE employee_registration_id = sa.employee_registration_id
AND allowance_type_id = sa.special_allowance_type_id
AND relation=(CASE WHEN sad.allowance_for='Child' THEN 'Children' ELSE sad.allowance_for END)
AND allowance_category_id = 0
LIMIT 1
) AS all_percent
FROM `special_allowance_detail` sad
INNER JOIN `special_allowance` sa ON sa.`special_allowance_id` = sad.`special_allowance_id`
WHERE company_id = 3
AND approved_status = "Requested"
ORDER BY sa.`document_identity`
