This is probably the simplest thing, but I am not getting it. I have a simplified query here where I want the whole row of the most recent (MAX) date for each P_ID. I have a whole script here complete with a basic select. I know there should be a sub-query or join someplace here:CREATE TABLE #t (DayKey INT,P_ID VARCHAR(8),Seq VARCHAR(5))INSERT INTO #t([DayKey], [P_ID], [Seq])SELECT 20110101, N'1', N'Rec1' UNION ALLSELECT 20110102, N'1', N'Rec2' UNION ALLSELECT 20110512, N'1', N'Rec3' UNION ALLSELECT 20120522, N'1', N'Rec4' UNION ALLSELECT 20120522, N'2', N'Rec1' UNION ALLSELECT 20110301, N'3', N'Rec1' UNION ALLSELECT 20110621, N'3', N'Rec2' UNION ALLSELECT 20120521, N'4', N'Rec1' UNION ALLSELECT 20110704, N'5', N'Rec1' UNION ALLSELECT 20120813, N'5', N'Rec2' UNION ALLSELECT 20120902, N'6', N'Rec1' UNION ALLSELECT 20120915, N'7', N'Rec1' UNION ALLSELECT 20111011, N'7', N'Rec2' UNION ALLSELECT 20120522, N'7', N'Rec3' UNION ALLSELECT 20111111, N'7', N'Rec4'SELECT [DayKey] ,[P_ID] ,[Seq]FROM #tDROP TABLE #t
The results will be inserted into a table that has no DayKey and must have a unique P_ID, so there can only be one of each P_ID.Duane