The following SQL query will return all my programs that are in development or completed mode. The goal here is to get the latest state of all programs.
I use the following query to return all my program states
SELECT PK_ProgramState, FK_Program, State
FROM ProgramStates
I get the following results:
As seen by the yellow highlight in the colored rectangles of this image, I want those "FK_Program" records to be returned. The others who come before the last highlighted record state are not needed.
I can't seem to figure out how to do it ... All the queries I've been trying give me bogus results. All help is appreciated.
Thanks in advance.
SELECT s1.PK_ProgramStatee, s1.FK_Program, s1.State
FROM ProgramStates s1
inner join
(
SELECT max(PK_ProgramState) as mstate, FK_Program
FROM ProgramStates
group by FK_Program
) s2 on s2.mstate = s1.PK_ProgramState and s2.FK_Program = s1.FK_Program
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments