我是SQL的新手,并编写了下面的脚本。总体结果是我想要返回所有记录,除非他们在where子句中有一个排除。此外,我知道我已经在某处加入了连接,因为最初我收到了多个重复记录,但现在根本没有记录,因此可能是连接和where子句的组合。我已经盯着代码多年了,看不出我哪里出错了。有人可以帮帮我吗?
提前致谢...
SELECT
c.address_number,
c.contact_number as contact_number,
c.label_name as contact_name,
a.address as address,
a.postcode as postcode,
s.source as source,
s.source_desc as source_desc,
m.joined as member_join_date,
--m.membership_card_expires as member_expiry_date,
o.payment_method as payment_method,
pf.payment_frequency_desc as payment_frequency_desc,
pf.frequency as payment_frequency,
c.std_code as std_code,
c.telephone as phone_number,
gift_aid = CASE
WHEN gad.gift_aid_number IS NOT NULL then 'Y' else null end,
cp.first_payment_date as first_payment_date,
cp.last_payment_date as last_payment_date,
-- NUMBER OF DONATIONS IN THE LAST 12 MONTHS GOES HERE --
cp.value_of_payments as total_donation_amount
FROM
contacts c
INNER JOIN contact_addresses ca on c.contact_number = ca.contact_number and
c.address_number = ca.address_number
INNER JOIN addresses a on ca.address_number = a.address_number
INNER JOIN sources s on c.source = s.source
INNER JOIN contact_suppressions cs on c.contact_number = cs.contact_number
INNER JOIN orders o on c.contact_number = o.contact_number
INNER JOIN members m on c.contact_number = m.contact_number
INNER JOIN contact_categories cc on c.contact_number = cc.contact_number
INNER JOIN payment_frequencies pf on o.payment_frequency = pf.payment_frequency
INNER JOIN gift_aid_donations gad on c.contact_number = gad.contact_number
INNER JOIN contact_performances cp on c.contact_number = cp.contact_number
WHERE
-- EXCLUSIONS --
c.status not in ('DECD','AN','DU','GONE') and
cs.mailing_suppression not in ('NOMA','APP','DATA')and
cc.activity not in ('MAJDON','PATRON','PCD11','PCT','PCAD12','PCAD13','PCFI13','PCHR12','PCIA12','PCNH12','PCSL13','PCSK13','PCTC13') and
-- DEFAULT ADDRESS ONLY --
ca.historical = 'N' and
-- MEMEBRSHIP NOT CANCELLED --
m.cancellation_reason is NULL