UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Using Last Function: Not Finding Newest Records    
 
   
sup909
post May 8 2009, 02:24 PM
Post #1

UtterAccess Member
Posts: 31
From: Western Springs, Illinois



I am using the last function in my table to sort the most recent maintenance record for items in my database. Unfortunately my qry does not seem to be picking up new records that I add to the maintenance table.

Now, I am using lookups within my table to auto-fill predetermined types of maintenance. In my query I have my last function actually looking at the lookup table as that is the only thing that seems to be returning values oddly enough. When I point the lookup directly at the maintenance table itself it cannot recognize the field values.

Below is my SQL code.

CODE
SELECT    tblMaintenance.Link,

    Last(tblMaintenance.MYear) AS LastOfMYear,

    tblHistoryLookup.Type,

    tblHistoryLookup.[Life Expectancy]

FROM    tblRoadwayInfo

INNER JOIN

    (tblHistoryLookup

        INNER JOIN

            tblMaintenance

        ON     tblHistoryLookup.ID = tblMaintenance.[Maintenance Type])

ON    tblRoadwayInfo.Link = tblMaintenance.Link

GROUP BY

    tblMaintenance.Link,

    tblHistoryLookup.Type,

    tblHistoryLookup.[Life Expectancy];


Edited by: sup909 on Fri May 8 15:24:46 EDT 2009.

EDIT: SQL script to prevent horizontal scrolling. Doug

Edited by: DougY on Fri May 8 17:09:34 EDT 2009.
Go to the top of the page
 
+
LPurvis
post May 8 2009, 02:31 PM
Post #2

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



Though "Last" may have date implications based upon its name it refers to the order of selection of a record.
So you'll get the date of whatever was the last record that the engine selected for each group.

Since you're also selecting another row from tblMaintenance are you wanting them to be related, or just grouped as you have them (i.e. you're not wanting the one "last" row from that table related to tblHistoryLookup?)

If MYear is the only field you're wanting an aggregated result from then Max is enough for you.
If you want an entire record then Max will generally still play a part - but in conjunction with a subquery.

Cheers.
Go to the top of the page
 
+
sup909
post May 8 2009, 02:50 PM
Post #3

UtterAccess Member
Posts: 31
From: Western Springs, Illinois



You are correct in that I want the entire record. I actually don't want anything from tblHistoryLookup per se, since that only has about 5 records in it. That table is basically just populating a lookup field for the tblMaintenance.Type
Go to the top of the page
 
+
sup909
post May 8 2009, 04:04 PM
Post #4

UtterAccess Member
Posts: 31
From: Western Springs, Illinois



Ahh, I was able to fix the problem by creating a sub-qry. Essentially I did the sort by Last twice and it worked.

Thanks.
Go to the top of the page
 
+
Bob_L
post May 8 2009, 04:06 PM
Post #5

Utterly Banned
Posts: 7,038



QUOTE
Ahh, I was able to fix the problem by creating a sub-qry. Essentially I did the sort by Last twice and it worked.

Thanks.

I would like to stress DO NOT USE LAST! It "APPEARS" to work in your current case, but it WILL NOT ALWAYS work the way you think it will. It is one of the most misused functions in Access and one of the least useful.

I believe you should rethink your use of it.
Go to the top of the page
 
+
LPurvis
post May 9 2009, 06:02 AM
Post #6

UtterAccess Editor
Posts: 13,753
From: England (North East / South Yorks)



To confirm what Bob's saying (who was confirming what I said and so I'm re-confirming what I said originally :-s).

LAST is not the choice here. It is MAX you should be using.
Last does have it's uses - and it is somewhat predictable (as much as it's possible to be) but not what to use in these scenarios).
I'm not clear on what you're saying you've implemented on Last to see what you wanted, but if my earlier supposition was correct, then we'd need to know the final desired output is to be to offer the more standard, reliable solution).

Cheers.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 22nd May 2013 - 04:46 PM