[Solved] Return highest MAX "date/time" only

Creating tables and queries
Post Reply
User avatar
dewayne ashley
Posts: 11
Joined: Fri Mar 03, 2017 4:25 am

[Solved] Return highest MAX "date/time" only

Post by dewayne ashley »

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 :D

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 :crazy:
I only need the the most recent entry to be returned.


Any Ideas?
Thank you
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
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Return highest MAX "date/time" only

Post by FJCC »

Try something like

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" 
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.
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.
User avatar
dewayne ashley
Posts: 11
Joined: Fri Mar 03, 2017 4:25 am

Re: Return highest MAX "date/time" only

Post by dewayne ashley »

will try
Libreoffice 5.1.6.2 on Linux Mint 18
User avatar
dewayne ashley
Posts: 11
Joined: Fri Mar 03, 2017 4:25 am

Re: Return highest MAX "date/time" only

Post by dewayne ashley »

FJCC wrote:Try something like

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" 
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.
:bravo: Many Thanks, Exactly what I was hoping to find. Will mark as SOLVED.
Libreoffice 5.1.6.2 on Linux Mint 18
Post Reply