So I'm trying to pull data from a few tables which require a max function to pull the proper information out. The query I have works wonderfully except that there is a certain set of records it's not returning. Those records would be ones that are actually in the ProspectDeleteBlock table, but do not exist in the SegmentProspect table. I'd love to be able to include those records, but it seems like an inner join is created here from my left joins somehow which is causing only records that exist in both ProspectDeleteBlock AND SegmentProspect to be shown. I'm just not seeing the issue in my joins or my statements. Any help appreciated!
Thanks,
-DSaba
SQL:
select distinct p.CycleID, sp.SegmentCode, RuleName, pc.HeliosKey, P.recordtype,
case
when CellPhone is null then 'N' else 'Y'
end CellPhone,
case
when BusinessPhone is null then 'N' else 'Y'
end BusPhone,
case
when EmailAddress is null then 'N' else 'Y'
end Email,
case
when BusinessEmail is null then 'N' else 'Y'
end Bemail,
ProspectPreferredGradYear,
LastGiftAmount, convert(char, LastGiftDate, 112) as LastGiftDate,
PhoneAppendOutcome, NCOAOutcome
from Prospect P
left outer join ProspectDeleteBlock pdb on pdb.ProspectCycleID = p.ProspectCycleID
inner join DeleteBlock db on pdb.DeleteBlockID = db.DeleteBlockID
inner join DeleteBlockRule dbr on db.DeleteBlockRuleID = dbr.DeleteBlockRuleID
inner join ProspectContainer pc on pc.ProspectID = p.ProspectID
left outer join SegmentProspect sp on p.ProspectCycleID = sp.ProspectCycleID
inner join Segment s on s.SegmentationID = sp.SegmentationID and sp.SegmentCode = s.SegmentCode
where p.CycleID =540
and Priority = (
select MIN(priority) from ProspectDeleteBlock pdb1
inner join DeleteBlock db1 on pdb1.DeleteBlockID = db1.DeleteBlockID
where pdb1.ProspectCycleID = pdb.ProspectCycleID
)
and SegmentRank = (
select MIN(s1.SegmentRank) from SegmentProspect sp1
inner join Segment s1 on s1.SegmentationID = sp1.SegmentationID and s1.SegmentCode = sp1.SegmentCode
where sp1.ProspectCycleID = sp.ProspectCycleID
)
order by pc.HeliosKey
Thanks,
-DSaba