我想编写一个查询以仅返回每个 documentID 的 PeriodID 为 BIGGEST 的行,例如:
Table1: DOCUMENT
DocumentID ItemName
-------------------------
1 Doc1
1 Doc1
1 Doc1
2 Doc2
3 Doc3
Table2: DOC_PERIOD
PeriodID StartDate EndDate DocumentID
----------------------------------------------------
20 1/1/2001 2/2/2002 1
30 2/1/2001 2/3/2012 1
40 3/2/2012 8/5/2012 1
25 3/2/2012 8/5/2012 2
35 3/2/2012 8/5/2012 2
45 3/2/2012 8/5/2012 3
EXPECTED OUTPUT
DocumentID ItemName PeriodID StartDate EndDate
--------------------------------------------------------------------------
1 Doc1 40 3/2/2012 8/5/2012
2 Doc2 35 3/2/2012 8/5/2012
3 Doc3 45 3/2/2012 8/5/2012
SELECT * FROM DOCUMENT
INNER JOIN DOC_PERIOD ON DOCUMENT.DocumentID = DOC_PERIOD.DocumentID
-- SCRIPT TO GET THE MAX PERIODID HERE
我的查询应该是什么样子?