Share via


Count 関数 (Microsoft Access SQL)

適用先: Access 2013 | Access 2016

クエリが返したレコードの数を計算します。

構文

Count(expr)

expr プレースホルダーは、カウントするデータを含むフィールドを識別する文字列式、またはフィールド内のデータを使用して計算を実行する式を表します。 expr のオペランドには、テーブル フィールドまたは関数の名前を含めることができます (組み込み関数またはユーザー定義関数を使用できますが、他の SQL 集計関数は含まれません)。 テキストを含め、あらゆる種類のデータをカウントできます。

注釈

基になるクエリのレコード数をカウントするには、 Count を使用します。 たとえば、 Count を使用して、特定の国または地域に出荷された注文の数をカウントできます。

expr はフィールドに対して計算を実行できますが、Count は単にレコードの数を集計します。 レコードに格納される値は関係ありません。

Expr がアスタリスク (*) ワイルドカード文字でない限り、Count 関数は Null フィールドを持つレコードをカウントしません。 If you use an asterisk, Count calculates the total number of records, including those that contain Null fields. Count( * )Count( [ 列名 ] ) よりもかなり高速 です。 Do not enclose the asterisk in quotation marks (' ').

The following example calculates the number of records in the Orders table:

SELECT Count(*) 
AS TotalOrders FROM Orders;

expr が複数のフィールドを識別する場合、Count 関数は、少なくとも 1 つのフィールドが Null でない場合にのみレコードをカウントします。 If all of the specified fields are Null, the record is not counted. フィールド名はアンパサンド (&) で区切ります。 The following example shows how you can limit the count to records in which either ShippedDate or Freight is not Null:

SELECT 
Count('ShippedDate & Freight') 
AS [Not Null] FROM Orders;

クエリ式で Count を使用します。 この式は、QueryDef オブジェクトの SQL プロパティ、または SQL クエリに基づいて Recordset オブジェクトを作成するときにも使用できます。

次の使用例では、Orders テーブルを使用して、英国に出荷された受注品の数を計算します。

この例では、EnumFields プロシージャを呼び出します。EnumFields プロシージャについては、SELECT ステートメントの使用例を参照してください。

Sub CountX() 
 
    Dim dbs As Database, rst As Recordset 
 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
    
    ' Calculate the number of orders shipped  
    ' to the United Kingdom. 
    Set rst = dbs.OpenRecordset("SELECT" _ 
        & " Count (ShipCountry)" _ 
        & " AS [UK Orders] FROM Orders" _ 
        & " WHERE ShipCountry = 'UK';") 
     
    ' Populate the Recordset. 
    rst.MoveLast 
     
    ' Call EnumFields to print the contents of the  
    ' Recordset. Pass the Recordset object and desired 
    ' field width. 
    EnumFields rst, 25 
 
    dbs.Close 
 
End Sub 

関連項目

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。