Have a Table that stores "ID(Primary Key AUTO)" "Id #" "Date / Time" "Location".
When a Id # is moved to a new Location I do not want to update its Location, I want to insert a new record, thus you can see if needed, where it was then and where it is now. Simple
I am having lots of trouble designing a query that can return only 1 current Location per Id # by Max "Date/Time" because when function MAX the "Date / Time" column , I also have to Group the rest of the columns which in turn returns the other entries for the same Id # also
I only need the the most recent entry to be returned.
Any Ideas?
Thank you
[Solved] Return highest MAX "date/time" only
- dewayne ashley
- Posts: 11
- Joined: Fri Mar 03, 2017 4:25 am
[Solved] Return highest MAX "date/time" only
Last edited by dewayne ashley on Sun May 07, 2017 4:09 am, edited 1 time in total.
Libreoffice 5.1.6.2 on Linux Mint 18
Re: Return highest MAX "date/time" only
Try something like
The sub query finds the max date for each id and the INNEr JOIN filters the main table to return just those combinations of ids and dates.
Code: Select all
SELECT "Id #", "Date / Time", "Location"
FROM "MyTable" As "MainTable" INNER JOIN
(SELECT "Id #", MAX("Date / Time") As "MaxDate" FROM "MyTable"
GROUP BY "Id #") AS "MaxQuery"
ON "MainTable"."Id #" = "MaxQuery"."Id #" AND "MainTable"."Date / Time" = "MaxQuery"."MaxDate"
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
- dewayne ashley
- Posts: 11
- Joined: Fri Mar 03, 2017 4:25 am
- dewayne ashley
- Posts: 11
- Joined: Fri Mar 03, 2017 4:25 am
Re: Return highest MAX "date/time" only
Many Thanks, Exactly what I was hoping to find. Will mark as SOLVED.FJCC wrote:Try something likeThe sub query finds the max date for each id and the INNEr JOIN filters the main table to return just those combinations of ids and dates.Code: Select all
SELECT "Id #", "Date / Time", "Location" FROM "MyTable" As "MainTable" INNER JOIN (SELECT "Id #", MAX("Date / Time") As "MaxDate" FROM "MyTable" GROUP BY "Id #") AS "MaxQuery" ON "MainTable"."Id #" = "MaxQuery"."Id #" AND "MainTable"."Date / Time" = "MaxQuery"."MaxDate"
Libreoffice 5.1.6.2 on Linux Mint 18