/*
Alias: WORK
LiveAnswer: FALSE

*/

SELECT PatEconomics.DentistID, PatEconomics.SessionID,
		SUM(PatEconomics.Credit) AS Total, SUM(PatEconomics.CreditEURO) AS TotalEURO,
		cast("1970-01-01" as date) as LastPayDate
FROM PatEconomics 
WHERE PatEconomics.sign = 1
GROUP BY PatEconomics.DentistID, PatEconomics.SessionID, PatEconomics.sign
UNION      
SELECT PatEconomics.DentistID, PatEconomics.SessionID,
		SUM(-PatEconomics.Credit) AS Total, SUM(-PatEconomics.CreditEURO) AS TotalEURO,
		MAX(PatEconomics."Date")  as LastPayDate
FROM PatEconomics 
WHERE PatEconomics.sign = -1
GROUP BY PatEconomics.DentistID, PatEconomics.SessionID, PatEconomics.sign
