I have a table tracking which contains
id | name | trackingNumber |
---|---|---|
1 | John Doe | 1Z21343V43DF4434 |
2 | John Doe | 1Z21343V43DF4435 |
3 | Jane Smith | 1Z21343V43DF4436 |
4 | Mark Jacobs | 1Z21343V43DF4436 |
5 | Mark Jacobs | 1Z21343V43DF4437 |
6 | Mark Jacobs | 1Z21343V43DF4438 |
and a table orders which contains
id | OrderNumber | Name |
---|---|---|
1 | 12-12324232 | John Doe |
2 | 12-12324234 | Mark Jacobs |
3 | 12-12324236 | Michael Smith |
when I run the query
select * from tracking t
join orders o
on (t.name = o.name OR o.name = t.name)
I get multiple results for John Doe and Mark Jacobs since they both have a multi-piece shipment. I cannot do a limit because in real world scenario there will be over 100 unshipped orders and 100s of tracking numbers.
I want to be able to get only the latest tracking number so the order does not get updated multiple times with all of the tracking numbers as one tracking number will show them all.
SQL Fiddle of current query - http://www.sqlfiddle.com/#!9/569d4a/1/0 I only want the latest trackingnumber for each name.