-- Select recipients, exclude people that have received an annual receipt
select p.p_partner_key_n AS PartnerKey, p.p_partner_short_name_c AS PartnerName, p.p_partner_class_c,
l.p_street_name_c, l.p_city_c, l.p_postal_code_c, l.p_country_code_c,
m.p_value_c as email, p_send_mail_l as validaddress
from p_partner p
-- select the email address. we only have on per partner at the moment
LEFT OUTER JOIN p_partner_attribute m ON (p.p_partner_key_n = m.p_partner_key_n AND m.p_attribute_type_c = 'E-Mail'),
p_subscription s, p_partner_location pl, p_location l
where p.p_partner_key_n = s.p_partner_key_n
and s.p_publication_code_c = {PublicationCode}
and s.p_subscription_status_c = 'PERMANENT'
and (s.p_date_cancelled_d IS NULL OR p_date_cancelled_d > CURRENT_DATE)
and (s.p_expiry_date_d IS NULL OR s.p_expiry_date_d > CURRENT_DATE)
and p.p_status_code_c = 'ACTIVE'
-- we only have one location per partner at the moment
AND p.p_partner_key_n = pl.p_partner_key_n
AND pl.p_location_key_i = l.p_location_key_i
AND pl.p_site_key_n = l.p_site_key_n
-- exclude donors with annual receipt because they get the annual letter anyways
AND NOT EXISTS
(select distinct p2.p_partner_key_n
from p_partner p2, a_gift g
WHERE g.p_donor_key_n = p2.p_partner_key_n
AND p2.p_receipt_letter_frequency_c = 'Annual'
AND g.a_date_entered_d between {DonationStartDate} and {DonationEndDate}
and p2.p_partner_key_n = p.p_partner_key_n)
AND p.p_no_solicitations_l = 0
ORDER BY validaddress, p_city_c, email;
PartnerKey
Valid Post Address
PartnerName
StreetName
PostCode
City
Country
E-Mail
{PartnerKey}
{validaddress}
{PartnerName}
{p_street_name_c}
{p_postal_code_c}
{p_city_c}
{p_country_code_c}
{email},