Spring Data,JPQL,按日期列的年份和月份分组

时间:2017-06-06 12:03:35

标签: java mysql spring spring-mvc stored-procedures

我有一个工作的原生MySQL查询,通过按年和月分组日期列来获取一些数据(例如:2017-01,2017-02)

SELECT 
    DATE_FORMAT(production.production_date,'%Y-%m') AS 'month', 
    SUM(operation.planned_quantity) AS 'planned_total', 
    SUM(operation.actual_quantity) AS 'actual_total', 
    ((SUM(operation.actual_quantity) / SUM(operation.planned_quantity)) *100) AS 'adherence' 
FROM operation 
INNER JOIN production on production.id = operation.production_id 
INNER JOIN control_point on control_point.id = production.control_point_id 
INNER JOIN work_center on work_center.id = control_point.work_center_id 
INNER JOIN cost_center on cost_center.id = work_center.cost_center_id 
INNER JOIN section on section.id = cost_center.section_id 
WHERE production.production_date BETWEEN '2017-01-01 00:00' AND '2017-05-31 23:59' 
    AND section.id = 4
GROUP BY DATE_FORMAT(production.production_date,'%Y-%m')

有了这个,我可以按预期得到输出 the result looks like this

现在我尝试了两天将此结果导入我的春季启动项目。尝试命名的本机查询,存储过程和方法签名模式。但每次尝试都失败了。最后我不得不使用我的git客户端丢弃更改。

我想要实现的是按部分日期分组,将结果映射到自定义Pojo或获取行MySQL结果到spring应用程序。

请帮助我。

  • 有没有办法在JPql中按部分日期分组,例如" GROUP BY DATE_FORMAT(production.production_date,'%Y-%米')"在MySQL中,如果是的话 请问?
  • 如何获取行MySQL结果作为列表或与spring数据中的本机查询相关?
  • 有关如何将存储过程结果映射到spring数据中的自定义POJO的任何示例?

------------------- 2017-06-07 --------------- <登记/> 解决问题
我设法通过在JPQL中混合MySQL函数 DATE_FORMAT(xxx,&#39;%Y-%m&#39;)来实现这个要求我真的不知道混合两者,但它的工作原理。 我的弹簧功能和查询:

@Query(value = "SELECT "
        + " new com.trendsmixed.fma.dao.MonthlyScheduleAdherence(DATE_FORMAT(operation.production.productionDate,'%Y-%m'), SUM(operation.actualQuantity), SUM(operation.plannedQuantity), (SUM(operation.actualQuantity)/SUM(operation.plannedQuantity))*100) "
        + " FROM Operation operation" 
        + " WHERE operation.production.productionDate BETWEEN :startDate AND :endDate"
        + " AND operation.production.controlPoint.workCenter.costCenter.section= :section"
        + " GROUP BY DATE_FORMAT(operation.production.productionDate,'%Y-%m')")
public List getMonthlyScheduleAdherenceBySection(@Param("startDate") Date startDate, @Param("endDate") Date endDate, @Param("section") Section section);

POJO映射结果:

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class MonthlyScheduleAdherence {

    String month;
    long actualTotal;
    long plannedTotal;
    long adherence;

}

我得到了:

[
    {
        month: "2017-02",
        actualTotal: 8640,
        plannedTotal: 10664,
        adherence: 81
    },
    {
        month: "2017-03",
        actualTotal: 8907,
        plannedTotal: 10352,
        adherence: 86
    },
    {
        month: "2017-04",
        actualTotal: 3224,
        plannedTotal: 4164,
        adherence: 77
    },
    {
        month: "2017-05",
        actualTotal: 8545,
        plannedTotal: 12435,
        adherence: 68
    }
]

这就是我想要的。 我成功地用这些数据制作了这个chart

谢谢

1 个答案:

答案 0 :(得分:1)

Sql函数在JPQL中可用。我的JPA版本是1.11.9。您的DATE_FORMAT函数不适用于我。我按天使用功能查询分组:

@Query(value = "SELECT count(ac) as count, function('date_format', max(ac.subscriptionStartDate), '%Y, %m, %d') as date FROM MyTable ac " +
        "WHERE ac.subscriptionStartDate BETWEEN :startDate AND :endDate GROUP BY function('date_format', ac.subscriptionStartDate, '%Y, %m, %d')")
public List<Map<String,Object>> findRegisteredCustomersHistory(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

查询结果是一个列表,该列表以格式将按天分组的记录数记录下来。

示例结果行:

count: 3, date: 2019, 09, 10
count: 1, date: 2019, 09, 11

JPQL中的简单函数查询:

@Query(value = "SELECT function('date_format', s.date, '%Y, %m, %d') as date from Transactions s;")