多个 AggregateResult 查询
Multiple AggregateResult Querys
大家好,
我目前正在尝试在同一个查询或结果中加入两个对象。
我的问题是是否可以显示或调试来自 LEAD 的 FIELD A 的总和 + 来自两个不同对象的 FIELD B 的总和。
这是我正在处理的示例:
顺便说一句,我真的很感谢你的时间和评论,如果我犯了错误,请告诉我,谢谢。
public static void example() {
String sQueryOne;
String sQueryTwo;
AggregateResult[] objOne;
AggregateResult[] objTwo;
//I tried to save the following querys into a sObject List
List<SObject> bothObjects = new List<SObject>();
sQueryOne = 'Select Count(Id) records, Sum(FieldA) fieldNA From Lead';
objOne = Database.query(sQueryOne);
sQueryTwo = 'Select Count(Id) records, Sum(FieldA) fieldNB From Opportunity';
objTwo = Database.query(sQueryTwo);
bothObjects.addAll(objOne);
bothObjects.addAll(objTwo);
for(sObject totalRec : bothObjects) {
//There's a Wrapper(className) I created which contains some variables(totalSum)
className finalRes = new className();
finalRes.totalSum = (Integer.valueOf(fieldNA)) + (Integer.valueOf(fieldNB));
System.debug('The sum is: '+finalRes.totalSum);
For example if I call a System debug with the previous variable finalRes.totalSum it's just showing the first value(fieldNA) duplicated.
以下调试显示了 sObject 列表的当前值,我想对其求和,例如 FIELD0 = 来自潜在客户,FIELD0 = 来自机会。
}
}
您通过调用 get('columnAlias')
访问 AggregateResult
中的列。如果您没有指定别名,它们将被 SF 自动编号为 expr0
、expr1
... 如有疑问,您可以随时使用 System.debug(results);
这可能会给你一些想法:
List<AggregateResult> results = new List<AggregateResult>{
[SELECT COUNT(Id) records, SUM(NumberOfEmployees) fieldA, SUM(AnnualRevenue) fieldB FROM Account],
[SELECT COUNT(Id) records, SUM(Amount) fieldA, SUM(TotalOpportunityQuantity) fieldB FROM Opportunity],
[SELECT COUNT(Id) records, SUM(NumberOfEmployees) fieldA, SUM(AnnualRevenue) fieldB FROM Lead]
/* hey, not my fault these are only 2 standard numeric fields on Lead.
It doesn't matter that they're identical to Account fields, what matters is what their SUM(...) aliases are
*/
};
List<Decimal> totals = new List<Decimal>{0,0,0};
for(AggregateResult ar : results){
totals[0] += (Decimal) ar.get('records');
totals[1] += (Decimal) ar.get('fieldA');
totals[2] += (Decimal) ar.get('fieldB');
}
System.debug(totals); // (636, 8875206.0, 9819762558.0) in my dev org
(我并不是说它是完美的,你的包装器 class 听起来是个更好的主意,甚至可能 Map<String, Decimal>
。取决于你打算如何处理结果)
大家好,
我目前正在尝试在同一个查询或结果中加入两个对象。
我的问题是是否可以显示或调试来自 LEAD 的 FIELD A 的总和 + 来自两个不同对象的 FIELD B 的总和。
这是我正在处理的示例:
顺便说一句,我真的很感谢你的时间和评论,如果我犯了错误,请告诉我,谢谢。
public static void example() {
String sQueryOne;
String sQueryTwo;
AggregateResult[] objOne;
AggregateResult[] objTwo;
//I tried to save the following querys into a sObject List
List<SObject> bothObjects = new List<SObject>();
sQueryOne = 'Select Count(Id) records, Sum(FieldA) fieldNA From Lead';
objOne = Database.query(sQueryOne);
sQueryTwo = 'Select Count(Id) records, Sum(FieldA) fieldNB From Opportunity';
objTwo = Database.query(sQueryTwo);
bothObjects.addAll(objOne);
bothObjects.addAll(objTwo);
for(sObject totalRec : bothObjects) {
//There's a Wrapper(className) I created which contains some variables(totalSum)
className finalRes = new className();
finalRes.totalSum = (Integer.valueOf(fieldNA)) + (Integer.valueOf(fieldNB));
System.debug('The sum is: '+finalRes.totalSum);
For example if I call a System debug with the previous variable finalRes.totalSum it's just showing the first value(fieldNA) duplicated.
以下调试显示了 sObject 列表的当前值,我想对其求和,例如 FIELD0 = 来自潜在客户,FIELD0 = 来自机会。
}
}
您通过调用 get('columnAlias')
访问 AggregateResult
中的列。如果您没有指定别名,它们将被 SF 自动编号为 expr0
、expr1
... 如有疑问,您可以随时使用 System.debug(results);
这可能会给你一些想法:
List<AggregateResult> results = new List<AggregateResult>{
[SELECT COUNT(Id) records, SUM(NumberOfEmployees) fieldA, SUM(AnnualRevenue) fieldB FROM Account],
[SELECT COUNT(Id) records, SUM(Amount) fieldA, SUM(TotalOpportunityQuantity) fieldB FROM Opportunity],
[SELECT COUNT(Id) records, SUM(NumberOfEmployees) fieldA, SUM(AnnualRevenue) fieldB FROM Lead]
/* hey, not my fault these are only 2 standard numeric fields on Lead.
It doesn't matter that they're identical to Account fields, what matters is what their SUM(...) aliases are
*/
};
List<Decimal> totals = new List<Decimal>{0,0,0};
for(AggregateResult ar : results){
totals[0] += (Decimal) ar.get('records');
totals[1] += (Decimal) ar.get('fieldA');
totals[2] += (Decimal) ar.get('fieldB');
}
System.debug(totals); // (636, 8875206.0, 9819762558.0) in my dev org
(我并不是说它是完美的,你的包装器 class 听起来是个更好的主意,甚至可能 Map<String, Decimal>
。取决于你打算如何处理结果)