How to return the last record in a table with multiple states

Alex

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:

Program states

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.

juergen d
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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return last record from database in codeigniter?

From Dev

How to find the last record of an associated table in cakephp?

From Dev

How to select last record by date in a joined table

From Dev

how to get last inserted record in a table?

From Dev

How to find a last record for a record in first table in mysql

From Dev

Get last record by date for multiple table with many-to-one relationship

From Dev

How to return boolean if record exists for multiple tables

From Dev

how to return multiple record sets using json

From Dev

Combine colums from table return multiple times the same record

From Dev

How to get the last record from Ms Access table

From Dev

How to get last 5 mins record from SQL database table?

From Dev

How to get the last record from Ms Access table

From Dev

How to get last 5 mins record from SQL database table?

From Dev

How to get Last and Secondlast Record from mysql table

From Dev

How do I get the second last record of a table in laravel

From Dev

how get last inserted id in multiple record insert?

From Dev

How to Select max value or last record when grouping multiple columns?

From Dev

Get last record in mysql table

From Dev

How to return null value if no record found on joined table

From Dev

How to return ordered data from multiple records into one record in MySQL?

From Dev

How to return ordered data from multiple records into one record in MySQL?

From Dev

How to query multiple linked record to return one row in SQL

From Dev

Return the average for each record in table

From Dev

Recordset loop doesn't return the last record

From Dev

How to select records of a table with last related record on another table in T-sql

From Dev

How to get record from multiple table using order by

From Dev

How to delete record for multiple table in one query using oracle

From Dev

How to get first record and last record in a date

From Dev

how to get last 7 days record in mysql if no value in particular day return 0

Related Related

  1. 1

    How to return last record from database in codeigniter?

  2. 2

    How to find the last record of an associated table in cakephp?

  3. 3

    How to select last record by date in a joined table

  4. 4

    how to get last inserted record in a table?

  5. 5

    How to find a last record for a record in first table in mysql

  6. 6

    Get last record by date for multiple table with many-to-one relationship

  7. 7

    How to return boolean if record exists for multiple tables

  8. 8

    how to return multiple record sets using json

  9. 9

    Combine colums from table return multiple times the same record

  10. 10

    How to get the last record from Ms Access table

  11. 11

    How to get last 5 mins record from SQL database table?

  12. 12

    How to get the last record from Ms Access table

  13. 13

    How to get last 5 mins record from SQL database table?

  14. 14

    How to get Last and Secondlast Record from mysql table

  15. 15

    How do I get the second last record of a table in laravel

  16. 16

    how get last inserted id in multiple record insert?

  17. 17

    How to Select max value or last record when grouping multiple columns?

  18. 18

    Get last record in mysql table

  19. 19

    How to return null value if no record found on joined table

  20. 20

    How to return ordered data from multiple records into one record in MySQL?

  21. 21

    How to return ordered data from multiple records into one record in MySQL?

  22. 22

    How to query multiple linked record to return one row in SQL

  23. 23

    Return the average for each record in table

  24. 24

    Recordset loop doesn't return the last record

  25. 25

    How to select records of a table with last related record on another table in T-sql

  26. 26

    How to get record from multiple table using order by

  27. 27

    How to delete record for multiple table in one query using oracle

  28. 28

    How to get first record and last record in a date

  29. 29

    how to get last 7 days record in mysql if no value in particular day return 0

HotTag

Archive