【问题标题】:How to get last six month data from database by month base如何按月从数据库中获取最近六个月的数据
【发布时间】:2016-12-19 02:54:17
【问题描述】:

我尝试过立即使用日历和 for 循环

for(int i = 6; i >= 0; --i){
    GraphDto graphDto=new GraphDto();

    Calendar c2=Calendar.getInstance();
    c2.add(Calendar.MONTH,-i);
    c2.set(Calendar.DAY_OF_MONTH, 1);

    graphDto.setLabel(getMonth(c2.get(Calendar.MONTH)));

    Date fromDate1=c2.getTime();
    c2.set(Calendar.DAY_OF_MONTH;
    c2.getActualMaximum(Calendar.DAY_OF_MONTH));

    Date toDate1=c2.getTime();
    Double value1=billHistoryRepository.findAmount(fromDate1,toDate1);
    Double value2=paymentHistoryRepository.findAmount(fromDate1,toDate1);
    c2.add(Calendar.MONTH,1);

    if(value1!=null) {
        graphDto.setValue1(value1);
    }
    if(value2!=null) {
        graphDto.setValue2(value2);
        graphDtoList.add(graphDto);
    }
}

花费太多时间并反复调用存储库。有没有其他方法可以查到,比如jdbc查询什么的

【问题讨论】:

标签: java mysql rest spring-boot spring-data-jpa


【解决方案1】:

我知道了..

select DATE_FORMAT(created_date,'%M %Y') as Month,sum(amount) from bill_history group by month(created_date) limit 6;

结果将是

月|总和(金额)

2016 年 6 月 | 129388

2016 年 7 月 | 22723419

2016 年 8 月 | 525004.4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    • 2022-01-03
    • 1970-01-01
    • 2017-08-06
    • 2019-08-16
    • 2020-09-17
    • 1970-01-01
    相关资源
    最近更新 更多