SELECT 0 as orderkey, history.mtype as mtype, history.clientid as clientid, ClientInfo.Firstname as firstname, ClientInfo.LastName as lastname, history.amount as amount, history.description as description, history.datereceived as datereceived FROM history JOIN ClientInfo ON history.clientid = clientinfo.clientid UNION SELECT 1 as orderkey, history.mtype, null as clientid, '' as firstname, '' as lastname, SUM(history.amount) as amount, 'total' as description, history.datereceived as datereceived FROM history JOIN ClientInfo ON history.clientid = clientinfo.clientid GROUP BY history.datereceived,history.mtype ORDER BY datereceived,orderkey