我以爲我有,但似乎我錯了。SQL最新日期
我有一個查詢
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'
這說明PrestatieCodes的列表,也複製。 現在我只需要一個沒有重複的PretatieCodes列表,它應該選擇最新的日期。 datecolumn = Uitvoerdatum。
我想我會嘗試這樣的:
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
這給我我想要的。然而,正如你可以注意到它沒有2列「Identificatie」和Foutmelding。如果我添加這些,它給我的錯誤
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.
所以我猜我的方法是錯誤的。我需要查看所有列,但只能查看最新的「PrestatieCode」。
我有什麼意義嗎?
+1:可以使用表別名 - 不太可能滾動屏幕。 – 2009-12-04 16:38:15
感謝您的回覆!我決定將不同的查詢拆分開來,因爲還有更多的參數需要加入。所以我使用了c#和querries的組合來完成這項工作,經過一些測試後,也給出了最好的表現。 – WtFudgE 2009-12-07 11:07:13