(十六)More SQL Function,select subject,max(score) as maxScore,avg(score)

205 篇文章 0 订阅

More SQL Function, more Group by

How to use ORM Bee develop when the sql like: select examno, subject,max(score) ,avg(score) ,min(score) from scores

How to use ORM Bee develop the functoin as below ?

select classno, term, examno, subject,max(score) as maxScore,avg(score) as avgScore,min(score) as minScore 
from scores where status!='FIN' group by term,examno,subjectno,subject order by classno,term,examno,subjectno

201     1     2020年秋八年级期末     语文     100     73.23     52     
201     1     2020年秋八年级期末     数学     100     71.25     49     
201     1     2020年秋八年级期末     英语     100     70.76     53

If you want to more function like : max(score) as maxScore,avg(score) as avgScore,min(score) as minScore,
You can develop with ORM Bee like below:

		Condition condition=new ConditionImpl();
		
		condition
		.selectField("classno,term,examno,subject")
		.selectFun(FunctionType.MAX, "score","maxScore")
		.selectFun(FunctionType.AVG, "score","avgScore")
		.selectFun(FunctionType.MIN, "score","minScore");
		
		condition.op("status", Op.nq, "FIN"); 
		
		condition
		.groupBy("term,examno,subjectno,subject")
		.orderBy("classno,term,examno,subjectno")
		;
		
		Scores scores=new Scores();
		String r=suidRich.selectJson(scores, condition); // json result
		System.out.println(r);
		
		List<String[]> listString=suidRich.selectString(scores, condition);  // string array result
		String str[];
		for (int i = 0; i < listString.size(); i++) {
			str=listString.get(i);
			for (int j = 0; j < str.length; j++) {
				System.out.print(str[j]+"     ");
			}
			System.out.println();
		}

You can get the result type as below :
json result
string array result
define a new response entity

List<ScoresResponse> list=suidRich.select(new ScoresResponse(), condition);


// ScoresResponse class :

@Entity("Scores")
public class ScoresResponse {
	
    //the field name same as Scores
    private String classno;
    private String term;
    private String examno;
    private String subject;
    private Double score;
    
    //the field name define in Condition
    private Double maxScore;
    private Double avgScore;
    private Double minScore;

    ......
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值