How to List Number of Records in Each Group of a SQL Group By

Last reviewed: July 31, 1995
Article ID: Q133023
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

SUMMARY

This article shows by example how to find out how many records occur in each group of a query that groups the records. You can do this by using the Query Designer or the SQL statement it creates.

MORE INFORMATION

Step-by-Step Example

This example uses the CUSTOMER.DBF table found in the VFP\SAMPLES\DATA subdirectory. The example groups the table by Country, and produces a cursor that has a record for each country with the number of records that appear in that country.

  1. On the File menu, click New, and then click Query. Base the query on VFP\SAMPLES\DATA\CUSTOMER.DBF.

  2. Select the Fields tab on the Query Designer. Drag the Customer.country field onto the Selected Output box. In the Functions/Expressions combo box, select the COUNT() function, to count the Customer.country field. Click the Add > button to move this expression into the Selected Output box.

  3. Select the Group By tab, and drag the Customer.country field onto the Group By Fields box.

  4. Click the SQL button on the Query Designer toolbar to see the SQL statement that was just created. It should look like this:

    SELECT customer.country, COUNT(customer.country);

          FROM testdata!customer;
          GROUP BY customer.country
    

  5. Close the SQL statement window. Click the exclamation button on the standard toolbar to run the query.


Additional reference words: 3.00 VFoxWin Structured Query Language
KBCategory: kbtool kbprg
KBSubcategory: FxtoolQueryvwdes


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: July 31, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.