How to Get Latest Invoice for Each Customer Number

Last reviewed: June 27, 1995
Article ID: Q105141
The information in this article applies to:
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, and 2.5a
  • Microsoft FoxPro for Windows, versions 2.5, and 2.5a

Customers frequently ask how to pull certain information out of a database that is the latest or most current information, usually based on some other field that may be duplicated in the same database.

While there are many different methods that might be employed, including using DO WHILE loops, creating a program or procedure with the following SELECT statements will achieve the quickest result (the INVOICES.DBF file in the TUTORIAL subdirectory has been used in this example because it gives a good approximation of this type of database structure):

   SELECT invoices.cno, MAX(invoices.idate) AS idate
      FROM invoices;
      GROUP BY invoices.cno;
      INTO cursor temp

   SELECT invoices.*;
      FROM invoices, temp;
      WHERE invoices.cno = temp.cno;
      AND invoices.idate = temp.idate;
      ORDER BY invoices.cno

These statements will automatically generate a Browse window with the desired result.


Additional reference words: FoxDos FoxWin 2.00 2.50 2.50a
KBCategory: kbprg
KBSubcategory: FxprgBrowse


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.