1.简介
公司想要根据年月分,去分某字段的总和。因为我们所用的jpa就写了简单的sql。
但是有一个发现了个问题,jpa呢是不能够直接根据你的模型返回所自定义的字段的。
就比如:
你的sql只返回其中一个或者两个字段,你是要用map去接返回值的。这也算是jpa不太方便的一个点吧。
不过很多大神研究源码发现,我们是可以用接口去接返回值的。为什么呢?留下你的关注吧,后期会出源码解析。
2. 简单的SQL
@Query(nativeQuery = true,
value = "select to_char(make_code_time, 'YYYY-MM') as makeCodeTime, sum(total_count) as totalCount \n" +
"from client_orders \n" +
"where (make_code_time >= ?1 or coalesce(?1, '') = '') \n" +
"and (make_code_time <= ?2 or coalesce(?2, '') = '') \n" +
"group makeCodeTime",
countQuery = "select count(1) \n" +
"from client_orders \n" +
"where (make_code_time >= ?1 or coalesce(?1, '') = '') \n" +
"and (make_code_time <= ?2 or coalesce(?2, '') = '') \n" +
"group by makeCodeTime")
Page<MakeCodeSumVo> getMonthMakeCode(Date startTime, Date endTime, Pageable pageable);
sql代码大概如上。切记要注意格式,注意格式,注意格式!!!
我这里是根据年月日获取码统计
查询结果格式:
| makeCodeTime | totalCount |
| 2020-07 | 3060 |
| 2020-06 | 10120 |
| 2020-05 | 32400 |
中国有句古话叫做”比葫芦画瓢“,相信你一定能得到你想要的。
😝 留下个关注吧、这里都是职场上用得上的,不是一些干货底层。
作者:Teddy (公众号:鸡仓故事汇)
ok!到这里就大功告成,小编(Teddy)在这里先感谢大家的到来。
虽然不是太详细,小编已经很努力,给小编来个一键三连(点赞,关注,收藏),小编会越来越努力。。。