MYSQL - How I can filter only Debtor and only Creditors using action_type column?
Test

SELECT -- client_id, 
       area_id, 
       SUM(CASE WHEN action_type='dr' 
                THEN amount END) total_debits,
       SUM(CASE WHEN action_type='cr' 
                THEN amount END) total_credits,
       SUM(CASE WHEN action_type='cr' 
                THEN amount END) - SUM(CASE WHEN action_type='dr' 
                                            THEN amount END) total_debtors,
       CASE WHEN SUM(CASE WHEN action_type='cr' 
                          THEN amount END) - SUM(CASE WHEN action_type='dr' 
                                                      THEN amount END) &gt; 0 
            THEN 'dr' 
            ELSE 'cr' END balance
FROM tbl_balancesheet
GROUP BY -- client_id, 
         area_id