SOQL聚合查询:计算返回的行数

Ric*_*d N 2 salesforce force.com soql apex-code

以下是我的SOQL查询:

select COUNT(Id) FROM Payroll_Group_Detail__c where Tax_Batch__c=null and CreatedDate >=2012-07-21T00:00:00-05:00 and Total_Tax_Amount__c!=null GROUP By Company__c,Name,Payment_Date__c,Pay_Cycle_Type__c;
Run Code Online (Sandbox Code Playgroud)

我正在尝试计算此group by子句返回的行数.但是我没有得到一个计数,而是获得了多行.如何获取此分组返回的行的总计数?

谢谢你,凯文

mas*_*t0r 9

只计算记录:

Integer counter = [ Select count() 
                    FROM Payroll_Group_Detail__c 
                    Where Tax_Batch__c = null 
                    And CreatedDate >= 2012-07-21T00:00:00-05:00 
                    And Total_Tax_Amount__c != null ];

System.debug('My counted records: ' + counter);
Run Code Online (Sandbox Code Playgroud)

使用GROUP BY:

AggregateResult[] aggr = [ Select count(Id) 
                           FROM Payroll_Group_Detail__c 
                           Where Tax_Batch__c = null 
                           And CreatedDate >= 2012-07-21T00:00:00-05:00 
                           And Total_Tax_Amount__c != null
                           Group By Total_Tax_Amount__c ];

Integer counter = Integer.valueOf(aggr.size());

System.debug('#### counter: ' + counter);
Run Code Online (Sandbox Code Playgroud)

但请记住,您不能超过允许的调控器限制(SOQL查询检索的记录总数 - > 50,000)