I'm trying to generate a report that shows how many Expense Reports (ERs) and the total of those reports for each employee for FY18. The issue I'm experiencing now is the SUM function. The way it is set now, it is summing distinct values in the field for each individual, not necessarily all the values for distinct rows. If I take the "distinct" out of sum(distinct X.ER_TOTAL_EXPENSE), that includes a lot of duplicates in the SUM result. I think it has something to do with my joins. How could I alter those to produce the desired result? I tried to follow the suggestions for this question- How do I SUM DISTINCT Rows?, but it did not work. Here's my current code:
select x.er_employee_number "UIN", x.er_user_full_name, p.PEBEMPL_COAS_CODE_HOME, count (distinct X.ER_DOCUMENT_ID) "Number of ERs", sum(distinct X.ER_TOTAL_EXPENSE) "ER Total"
from INFOR.ALER_EXPENSE_EXPORT_HEADER@TEMPROD x
LEFT JOIN spriden@REPTPROD s on x.ER_Employee_NUMBER = s.spriden_id
LEFT JOIN pebempl@REPTPROD p on s.SPRIDEN_PIDM = p.pebempl_pidm
where X.EXPORT_HDR_STATUS like '4'
and X.ER_PURPOSE like 'Employee%'
and NEW_TIME(X.EXPORT_HDR_STATUS_DATETIME, 'GMT', 'CST') > to_date('1-Jul-2017')
and NEW_TIME(X.EXPORT_HDR_STATUS_DATETIME, 'GMT', 'CST') < to_date('30-Jun-2018')
group by x.er_employee_number, x.er_user_full_name, p.PEBEMPL_COAS_CODE_HOME;
Here is an example of the results: The results on the left are accurate, the results on the right are what the above SQL generates. The ER Total is off by 24. That is because that employee had 2 transactions for $24.