UPDATE
As if my previous answer wasn't long enough ...
I'd also move the JOIN to the users table from the inner queries to the outer queries, it looks like it's identical in every case.
But, we need that for the calculation of BonusAmnt (which appears to always be based on whatever Total
is; the calculation is the same for all of them except for two. So, I also moved the BonusAmnt calculation to the outer query as well, and just adding a check for which calculation to do, using a CASE expression.
I may have missed something.
But here's how I'd write the query.
I'll show it in two pieces, with the query for the sub
inline view separate from the outer query.
SELECT TRUNCATE(sub.Total,3) AS GrpTotal
, sub.ActualDate
, CASE
WHEN sub.Report IN ('Month End Fee Rejects','Direct ACH Debits and Credits') THEN
TRUNCATE(sub.Total,3)
ELSE
TRUNCATE(sub.Total*IFNULL((u.retention_percent/100),1)+IFNULL(u.bonus_amount,0)),3)
END AS GrpBonusAmt
, sub.UID
, sub.CUSTID
, YEAR(sub.ActualDate) AS Year
, pusers.username
FROM (
-- query to produce sub goes here
) sub
LEFT
JOIN `users` u
ON u.uid = sub.UID
LEFT
JOIN `pending_users` pusers
ON pusers.UID = sub.UID
LEFT
JOIN `users` n
ON n.UID = sub.CUSTID
AND n.is_admin
WHERE n.UID IS NULL
ORDER BY sub.ActualDate, sub.UID, sub.Report
And here is the second part. This is the query that gets stuck into the middle of the part above, as an inline view. This is the query that that produces sub
row source for the outer query:
SELECT 'Global Report' AS Report
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.totalpayment) AS Total
FROM `globalreport` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Amex Residuals'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.payment) AS Total
FROM `amexresiduals` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Compliance Fee'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.profit) AS Total
FROM `compliancefee` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Checks On Demand'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.myprofit) AS Total
FROM `geticheck` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Gift Cards on Demand'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.payment) AS Total
FROM `gcod` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Global Check' AS Report
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.myprofit) AS Total
FROM `globalcheck` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Bonus True Up'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.finalpayment) AS Total
FROM `bonustrueup` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Bonus Take Back - Did Not Activate'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.amount) AS Total
FROM `bonusadjnosetup` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Bonus Take Back - Closed Less Than 6 Months'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.amount) AS Total
FROM `bonusadjclosed6mo` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Month End Fee Rejects'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.amount) AS Total
FROM `merchantloss` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Direct ACH Debits and Credits'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.amount*-1) AS Total
FROM `dirachdebcred` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID
UNION ALL
SELECT 'Merchant Adjustments'
, a.UID, a.ActualDate, a.CUSTID
, SUM(a.amount) AS Total
FROM `merchantadj` a
WHERE a.CUSTID = 1020
GROUP BY a.ActualDate, a.UID