HQL中的聚合函数:count()sum()avg()max()min()

**聚合函数:count()/sum()/avg()/max()/min()**

@org.junit.Test

	public void avg2(){
		//使用DecimalFormat方法对Double数值进行格式化
		DecimalFormat df=new DecimalFormat("0.00");		
		session=HibernateUtil.getSession();
		hql="select e.dept.deptName,avg(e.sal) from Emp e group by e.dept.deptName";
		List olist=session.createQuery(hql).list();
		for(Object[] o:olist){
			System.out.print(o[0]+" : ");
			Double avg=(Double) o[1];			
			System.out.print(df.format(avg));
			System.out.println();
		}
		HibernateUtil.closeSession();
	}
	public void avg(){
		//使用DecimalFormat方法对Double数值进行格式化
		DecimalFormat df=new DecimalFormat("0.00");		
		session=HibernateUtil.getSession();
		hql="select avg(e.sal) from Emp e";
		Double totalSal=(Double)session.createQuery(hql).uniqueResult();
		System.out.println(df.format(totalSal));
		HibernateUtil.closeSession();
	}
//session.createQuery(count-hql)取出的obj转换long-int的两种方法
public void count(){
		session=HibernateUtil.getSession();
		hql="select count(*) from Emp e";
	//	int num=(int)(long) session.createQuery(hql).uniqueResult();
		int num=((Long) session.createQuery(hql).uniqueResult()).intValue();
		System.out.println(num);
		HibernateUtil.closeSession();
	}

HQL中的聚合函数:count()sum()avg()max()min()_第1张图片

你可能感兴趣的:(Hibernate)