I thought I had it but it seemed I was wrong.
I have a query
select PrestatieCode, Uitvoerdatum, Identificatie, Foutmelding
from bam_Zending_AllInstances as zAll
left outer join bam_Zending_CompletedRelationships as zRel on
zAll.ActivityID = zRel.ActivityID
left outer join bam_Prestatie_AllInstances as pAll on
zRel.ReferenceData = pAll.ActivityID
where zAll.Zender = 'BL15'
and ReferenceType = 'Activity'
and Zendingnummer = '632'
This shows a list of PrestatieCodes, also duplicates. Now I only need to have a list of PretatieCodes without the duplicates, it should just pick the one with the lastest date. The datecolumn = Uitvoerdatum.
I thought I'd try something like:
select max(Uitvoerdatum), PrestatieCode
from bam_Zending_AllInstances as zAll
left outer join bam_Zending_CompletedRelationships as zRel on
zAll.ActivityID = zRel.ActivityID
left outer join bam_Prestatie_AllInstances as pAll on
zRel.ReferenceData = pAll.ActivityID
where zAll.Zender = 'BL15'
and ReferenceType = 'Activity'
and Zendingnummer = '632'
group by PrestatieCode
which gives me what I want. However as you can notice it does not have the 2 columns "Identificatie" and Foutmelding. If I add these it gives me the error
Column 'bam_Prestatie_AllInstances.Identificatie' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
So I'm guessing my approach is wrong. I need to see all the columns but only for the latest "PrestatieCode"s.
Am I making any sense?