這是你在找什麼?
DECLARE @Table TABLE (DocID INT, DocNumber VARCHAR(10), MilestoneDate DATETIME, MilestoneID INT)
INSERT INTO @Table VALUES (30, '14-1', '10-01-2001', 10)
INSERT INTO @Table VALUES (30, '14-1', '10-15-2001', 11)
INSERT INTO @Table VALUES (30, '14-1', '11-06-2002', 13)
INSERT INTO @Table VALUES (30, '14-1', '11-01-2003', 27)
INSERT INTO @Table VALUES (30, '14-1', '11-01-2003', 30)
INSERT INTO @Table VALUES (30, '14-1', '03-01-2004', 28)
INSERT INTO @Table VALUES (30, '14-1', '03-01-2004', 31)
INSERT INTO @Table VALUES (31, '14-3', '06-27-2003', 13)
INSERT INTO @Table VALUES (31, '14-3', '05-05-2004', 27)
INSERT INTO @Table VALUES (31, '14-3', '05-05-2004', 30)
INSERT INTO @Table VALUES (31, '14-3', '07-20-2005', 35)
INSERT INTO @Table VALUES (31, '14-3', '08-31-2005', 23)
INSERT INTO @Table VALUES (36, '1-2', '10-01-1996', 10)
INSERT INTO @Table VALUES (36, '1-2', '10-01-1996', 11)
INSERT INTO @Table VALUES (36, '1-2', '12-01-1996', 28)
INSERT INTO @Table VALUES (36, '1-2', '12-01-1996', 31)
-- get the items where we want the max
SELECT DocID, DocNumber, MAX(MilestoneDate) AS MilestoneDate
FROM @Table WHERE MilestoneID IN (10, 11)
GROUP BY DocID, DocNumber
UNION -- get the items where we want the min
SELECT DocID, DocNumber, MIN(MilestoneDate)
FROM @Table
WHERE DocID NOT IN (SELECT DocID FROM @Table WHERE MilestoneID IN (10, 11))
GROUP BY DocID, DocNumber
Result:
DocID DocNumber MilestoneDate
30 14-1 2001-10-15 00:00:00.000
31 14-3 2003-06-27 00:00:00.000
36 1-2 1996-10-01 00:00:00.000
你應該嘗試清理表(使用4位在啓動,使其保持你的格式,使用空格而不是製表符分隔),以及顯示您嘗試過的SELECT的示例。在現有代碼中顯示問題要比經常創建一個全新的查詢更容易。 – Guvante
向我們展示[你嘗試過的](http://www.whathaveyoutried.com)。 – Kermit
這是我試過的:select DocID,DocNumber, CASE當(MAX(MilestoneID)= 10或MAX(MilestoneID)= 11)和Not Max(MilestoneDate)爲空THEN Max(MilestoneDAte)END最早 FROM table 從 – Ddragov