How to select last record by date in a joined table

user813813

I have 2 tables, Car_Table and History_Table. I want to be able to select the last history item for all cars when their status is set to broken.

The tables are as follows

Car_Table
Car_ID  Driver_Name Car_Status
1       Alan        Broken
2       Dave        Broken

and

History_Table
id  Date        Notes       Car_Id
1   01-01-2017  Change oil      1
2   02-01-2017  Check Brakes    1
3   02-01-2017  Service         2
3   03-01-2017  Cleaning        2

When I do

select Car_Table.Driver_Name,History_Table.Notes from Car_Table 
inner join History_Table on Car_Table.Car_ID = History_Table.CarID 
where Car_Table.Car_Status = 'Broken'

I get all of the history records returned. Is there a way to get the last history item for each car that has a status of "broken" ?

Gurwinder Singh

You can find row with max date for each car using the left join technique and then join it with the Car_table:

select Car_Table.Driver_Name,
    History_Table.Notes
from Car_Table
inner join (
    select h1.*
    from History_Table h1
    left join History_Table h2 on h1.Car_ID = h2.Car_ID
        and h1.date < h2.date
    where h2.Car_ID is null
    ) History_Table on Car_Table.Car_ID = History_Table.CarID
where Car_Table.Car_Status = 'Broken';

See official MySQL site for alternate solutions (using aggregation in correlated and uncorrelated subquery, which are likely to be slower than the solution above).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select from joined table only if record exists

From Dev

select record from joined table if it exists

From Dev

How To Select One Top Of Each Record By Id in Second Joined Table In SQL Server SELECT?

From Dev

TOP 1 Record Based on Date for each record in joined table

From Dev

How to get first record and last record in a date

From Dev

Select the last record for the date and time columns

From Dev

SELECT Last record in a Month based on Users table

From Dev

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

From Dev

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

From Dev

Select min date record from duplicates in table

From Dev

How to select servers that does not have a date record in a separate table between a date range?

From Dev

How to get last 7 days record from table as per date using PHP and MySQL

From Dev

Select last record from data table for each device in devices table

From Dev

How to select last record in a LINQ GroupBy clause

From Dev

How to select the last record of each ID

From Dev

How to select the last rowspan in a table?

From Dev

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

From Dev

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

From Dev

how to get last inserted record in a table?

From Java

SQL How to select from multiple values in joined table

From Dev

How to select a minimal value from a joined table with JPA?

From Dev

Get first and last record number in every date exists in table

From Dev

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

From Dev

Multiple select for aleady joined table

From Dev

How can I get the next record date from a date and the last record date from a date?

From Dev

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

From Dev

How to select by MAX date in CI Active Record

From Dev

How to select record with latest time date stamp

From Dev

Check if matching record exists in joined table

Related Related

  1. 1

    Select from joined table only if record exists

  2. 2

    select record from joined table if it exists

  3. 3

    How To Select One Top Of Each Record By Id in Second Joined Table In SQL Server SELECT?

  4. 4

    TOP 1 Record Based on Date for each record in joined table

  5. 5

    How to get first record and last record in a date

  6. 6

    Select the last record for the date and time columns

  7. 7

    SELECT Last record in a Month based on Users table

  8. 8

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

  9. 9

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

  10. 10

    Select min date record from duplicates in table

  11. 11

    How to select servers that does not have a date record in a separate table between a date range?

  12. 12

    How to get last 7 days record from table as per date using PHP and MySQL

  13. 13

    Select last record from data table for each device in devices table

  14. 14

    How to select last record in a LINQ GroupBy clause

  15. 15

    How to select the last record of each ID

  16. 16

    How to select the last rowspan in a table?

  17. 17

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

  18. 18

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

  19. 19

    how to get last inserted record in a table?

  20. 20

    SQL How to select from multiple values in joined table

  21. 21

    How to select a minimal value from a joined table with JPA?

  22. 22

    Get first and last record number in every date exists in table

  23. 23

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

  24. 24

    Multiple select for aleady joined table

  25. 25

    How can I get the next record date from a date and the last record date from a date?

  26. 26

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

  27. 27

    How to select by MAX date in CI Active Record

  28. 28

    How to select record with latest time date stamp

  29. 29

    Check if matching record exists in joined table

HotTag

Archive