This query works by totaling distributions...so it is critical that all payments have been distributed.
This query totals all the amounts paid which meet these criteria:
- paid by a person
- not an estimated distribution (this should exclude Payroll Deducts)
- pmt date tween 1/1/2007 and 12/31/2007
- person paid a total of at least 250...this could have been in several payments
select *
from
(
with Paid as
(
select pl.NameID,
sum(di.DistributionAmount) AS TotalPaid
from camDistributions di, camPayments py, camPledges pl
where di.PaymentID = py.PaymentID
and di.PledgeID = pl.PledgeID
and py.PaymentDate >= '01-JAN-2007'
and py.PaymentDate <= '31-DEC-2007'
group by py.NameID
having sum(di.DistributionAmount) >= 250
)
select n.NameID, n.DisplayName, n.AddressLine1, n.AddressLine2, n.City, n.State, n.Zip, p.Salutation, pd.TotalPaid
from camNamesV n, camPeople p, Paid pd
where n.NameID = p.NameID
and n.NameID = pd.NameID
)