CREATE TABLE #TEST
(
Name varchar(10),
DateOfEvent date,
EventType varchar(10)
)
INSERT INTO #TEST (Name, DateOfEvent, EventType)
SELECT 'Smith', '10/1/2005', 'New' UNION ALL
SELECT 'Thomas', '1/1/2002', 'Updated' UNION ALL
SELECT 'Johnson', '6/1/2002', 'New' UNION ALL
SELECT 'Smith', '7/1/2008', 'Updated' UNION ALL
SELECT 'Smith', '7/1/2000', 'New'
SELECT NEW.* FROM
(SELECT Name, MAX(DateOfEvent) AS DateOfEvent
FROM #TEST
WHERE EventType = 'Updated'
GROUP BY Name
) UPDATED
JOIN
(SELECT Name, MIN(DateOfEvent) AS DateOfEvent
FROM #TEST
WHERE EventType = 'New'
GROUP BY Name
)New ON New.DateOfEvent < UPDATED.DateOfEvent AND New.Name = UPDATED.Name
不知道我理解你。鑑於上面的例子,預期的結果是什麼? – 2010-10-28 19:32:40