首页 技术 正文
技术 2022年11月8日
0 收藏 968 点赞 1,871 浏览 2946 个字

聚合 和 分组聚合:

PlayMorphia 它提供了基于开发人员models的友好接口

设想你定义了一个model。class Sales:

@Entity public class Sales extends Model {
public String employeeId;
public String department;
public String region;
public int amount;
}

聚合:如今你能够在Sales模型上做聚合操作,以下是一些详细演示样例:

long total = Sales.count(); // also Sales.q().count();
long cntIT = Sales.q().filter("department", "IT").count();
long cntAU = Sales.q().filter("region", "AU").count();
long cntAuIt = Sales.find("region, department", "AU", "IT").count();

求和:

long sum = Sales._sum("amount"); // also Sales.q().sum("amount");
long sumIT = Sales.find("department", "IT").sum("amount");
long sumAU = Sales.find("region", "AU").sum("amount");
long sumAuIt = Sales.find("region department", "AU", "IT").sum("amount");

最大值:

long max = Sales._max("amount"); // also Sales.q().max("amount");
long maxIT = Sales.find("department", "IT").max("amount");
long maxAU = Sales.find("region", "AU").max("amount");
long maxAuIt = Sales.find("region department", "AU", "IT").max("amount");

最小值:

long min = Sales._min("amount"); // also Sales.q().min("amount");
long minIT = Sales.find("department", "IT").min("amount");
long minAU = Sales.find("region", "AU").min("amount");
long minAuIt = Sales.find("region department", "AU", "IT").min("amount");

分组聚合:

每个聚合都会相应一个分组聚合的接口,就像SQL中的group by语句

分组计数:

// group by region
AggregationResult byRegion = Sales.groupCount("region");
System.out.println("AU count: " + byRegion.get("region", "AU");
// group by department
AggregationResult byDep = Sales.groupCount("department");
System.out.println("IT count: " + byDep.get("department", "IT");
// group by region and department
AggregationResult byRegionDep = Sales.groupCount("region, department");
System.out.println("IT count: " + byRegionDep.get("department, region", "IT", "AU");

分组求和:

// group by region
AggregationResult byRegion = Sales.groupSum("region");
System.out.println("AU sum: " + byRegion.get("region", "AU");
// group by department
AggregationResult byDep = Sales.groupSum("department");
System.out.println("IT sum: " + byDep.get("department", "IT");
// group by region and department
AggregationResult byRegionDep = Sales.groupSum("region, department");
System.out.println("IT sum: " + byRegionDep.get("department, region", "IT", "AU");

分组求最大值:

// group by region
AggregationResult byRegion = Sales.groupMax("region");
System.out.println("AU max: " + byRegion.get("region", "AU");
// group by department
AggregationResult byDep = Sales.groupMax("department");
System.out.println("IT max: " + byDep.get("department", "IT");
// group by region and department
AggregationResult byRegionDep = Sales.groupMax("region, department");
System.out.println("IT max: " + byRegionDep.get("department, region", "IT", "AU");

分组求最小值:

// group by region
AggregationResult byRegion = Sales.groupMin("region");
System.out.println("AU min: " + byRegion.get("region", "AU");
// group by department
AggregationResult byDep = Sales.groupMin("department");
System.out.println("IT min: " + byDep.get("department", "IT");
// group by region and department
AggregationResult byRegionDep = Sales.groupMin("region, department");
System.out.println("IT min: " + byRegionDep.get("department, region", "IT", "AU");

原文链接:http://www.playframework.com/modules/morphia-1.2.9/statistics

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894