翻譯頁面
本頁面由英文在 AI 協助下翻譯而成。若兩者有出入,請以英文版本為準。
COUNT
傳回資源中的記錄總數,或分組中某資料行非 NULL 值的個數。
另請參閱:
[SELECT]
語法
SELECT COUNT( * | columnExpression ) FROM <multipartIdentifier>
[ GROUP BY groupByColumn ];
引數
columnExpression 或 *
資料行或運算式。指定 * 時,會計算資源或分組中的所有記錄。
columnExpression 的
NULL值不會計入。若所有值都是NULL,COUNT會傳回0。
groupByColumn
用來執行摘要或彙總作業的一個或多個資料行。GROUP BY 子句會為每個資料行分組傳回一個資料列。
傳回值
傳回表示計數的整數。
範例
傳回整個資源中的資料列數
SELECT count(*) as num_instances
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';
傳回依資料行運算式分組的資料列數
SELECT SPLIT_PART(machineType, '/', -1) as machine_type, count(*) as num_instances
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a'
GROUP BY SPLIT_PART(machineType, '/', -1);
將 COUNT 作為視窗函式來計算累計計數
-- Track cumulative issue counts over time
SELECT
number,
title,
state,
created_at,
COUNT(*) OVER (ORDER BY created_at) as cumulative_issues,
COUNT(*) OVER (PARTITION BY state ORDER BY created_at) as cumulative_by_state
FROM github.issues.issues
WHERE owner = 'stackql'
AND repo = 'stackql'
ORDER BY created_at;
如需詳細資訊,請參閱 https://www.sqlite.org/lang_aggfunc.html#count。