利用SOQL 的Aggregate Functions (SUM,MAX,MIN...) 让代码更加的优雅,高效和稳定
2017-10-13 00:00
344 查看
1.没用Aggregate Functions的代码如下:
2.使用Aggregate Functions的代码如下:
利用Aggregate Functions 重写的代码2,更加易于阅读和高效
想象一下 :例如ObjectTerritory2Association 里面有1W条数据,ObjectId 分类只有3千
Set<Id> set_AccountIds = new Set<Id>(); map<Id,list<Datetime>> map_AccountId_LastModifiedDate = new map<Id,list<Datetime>>(); for(ObjectTerritory2Association OTTAobj: [SELECT ObjectId, LastModifiedDate FROM ObjectTerritory2Association]){ if(map_AccountId_LastModifiedDate.get(OTTAobj.ObjectId) == null) { map_AccountId_LastModifiedDate.put(OTTAobj.ObjectId, new list<Datetime>()); } map_AccountId_LastModifiedDate.get(OTTAobj.ObjectId).add(OTTAobj.LastModifiedDate); } for(Account account : [SELECT Id, Last_Batch_Job_Run_DateTime__c FROM Account WHERE Id IN :map_AccountId_LastModifiedDate.keyset()]){ map_AccountId_LastModifiedDate.get(account.Id).sort(); Datetime maxLastModifiedDate = map_AccountId_LastModifiedDate.get(account.Id).get(map_AccountId_LastModifiedDate.get(account.Id).size() -1); if(account.Last_Batch_Job_Run_DateTime__c == null){ set_AccountIds.add(account.Id); }else if(account.Last_Batch_Job_Run_DateTime__c < maxLastModifiedDate){ set_AccountIds.add(account.Id); } }
2.使用Aggregate Functions的代码如下:
Set<Id> accountIds = new Set<Id>(); Map<Id,Datetime> accountDateMap = new map<Id,Datetime>(); AggregateResult[] groupedResults = [SELECT ObjectId, Max(LastModifiedDate) FROM ObjectTerritory2Association GROUP BY ObjectId]; for (AggregateResult ar : groupedResults) { accountDateMap.put(String.valueOf(ar.get('ObjectId')), (Datetime)ar.get('expr0')); } for(Account account : [SELECT Id, Last_Batch_Job_Run_DateTime__c FROM Account WHERE Id IN :accountDateMap.keyset()]){ if(account.Last_Batch_Job_Run_DateTime__c == null){ accountIds.add(account.Id); }else if(account.Last_Batch_Job_Run_DateTime__c < accountDateMap.get(account.Id)){ accountIds.add(account.Id); } }
利用Aggregate Functions 重写的代码2,更加易于阅读和高效
想象一下 :例如ObjectTerritory2Association 里面有1W条数据,ObjectId 分类只有3千
相关文章推荐
- C# 中奇妙的函数–6. 五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- 巧用STL中的绑定器和函数对象嵌套调用实现优雅高效的代码
- Linq学习笔记(1.8)——Count、Sum、Min、Max、Average
- 步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- Oracle分析函数三——SUM,AVG,MIN,MAX,COUNT
- 脚本实现sum/avg/max/min-求文本中每行数字
- 7.8 Mathematical 算术和聚合操作 - Average/Concat/Count/Max/Min/Sum/Reduce
- 利用|,&,^,~,<<,>>>写出高效艺术的代码
- Hive分析窗口函数(一) SUM,AVG,MIN,MAX
- LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg
- 【HDU3415】Max Sum of Max-K-sub-sequence,思路+解题报告+AC代码+自虐般疯狂吐槽【0.3%达成!】
- 利用on和off方法编写高效的js代码
- C# 中奇妙的函数–五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
- 集合函数AVG,SUM,MAX,MIN
- List::Util, max(), min(), sum(), maxstr(), minstr()...
- 6.组函数(avg(),sum(),max(),min(),count())、多行函数,分组数据(group by,求各部门的平均工资),分组过滤(having和where),sql优化
- 编写更加稳定、可读性强的JavaScript代码
- 7、步步为营VS 2008 + .NET 3.5(7) - LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count、LongCount、Sum、Min、Max、Average、Aggregate、Cast、DefaultIfEmpty、SequenceEqual、OfType、ToArray、ToList、ToDictionary
- oracle中的聚合函数count、max、min、sum、avg等等
- LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg