Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / August 2006

Tip: Looking for answers? Try searching our database.

last row per account

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Knox - 31 Aug 2006 19:14 GMT
I have a table with an stock number (STOCK) and a sequence number (SEQNO)
and a bunch of other fields (TITLE is one). This table is maintained by a
proprietary software package.  I just read it.  The sequence number is
unique, but the stock is repeated multiple times.  In those cases where the
account appears multiple times, only the last row (as determined by sequence
number) is the good row.

When I want to access this row, I do this cumbersome query which groups by
stock and finds the maximum sequence for each stock number.  Then I have
another query which links to the first query by sequence number.  I then can
access the correct row.

Is there an easier way using the last function with grouping?  In other
words, could I just do a group by stock, last on all the other fields, order
by sequence and would that give me what I want?  I've stayed away from last
because I read somewhere it was not deterministic.

For example:

SELECT INVPLUS.STOCK, Last(INVPLUS.TITLE) AS LastOfTITLE
FROM INVPLUS
GROUP BY INVPLUS.STOCK
ORDER BY Max(INVPLUS.SEQNO);

That certains gives me the correct stock number but will the title be
correct?

Thanks in advance,

Knox
John Vinson - 31 Aug 2006 21:56 GMT
>When I want to access this row, I do this cumbersome query which groups by
>stock and finds the maximum sequence for each stock number.  Then I have
[quoted text clipped - 5 lines]
>by sequence and would that give me what I want?  I've stayed away from last
>because I read somewhere it was not deterministic.

Last() returns the last record *in disk storage order* - which is
pretty much uncontrollable. What I'd suggest is a Subquery instead:

SELECT INVPLUS.STOCK, INVPLUS.TITLE
FROM INVPLUS
WHERE INVPLUS.SEQNO =
 (SELECT MAX(Z.[SEQNO]) FROM INVPLUS AS Z
  WHERE Z.STOCK = INVPLUS.STOCK);

                 John W. Vinson[MVP]
Knox - 01 Sep 2006 00:48 GMT
>>When I want to access this row, I do this cumbersome query which groups by
>>stock and finds the maximum sequence for each stock number.  Then I have
[quoted text clipped - 19 lines]
>
>                  John W. Vinson[MVP]

Disk Storage Order...  Very interesting.  That's so unusable, they should
take it out of the language!  Thank you for explaining it and also for the
suggestion of the subquery.

Knox
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.