Thi*_*nga 5 java mysql spring stored-procedures spring-mvc
我有一个可用的本地 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')
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以按预期获得输出 结果看起来像这样
现在我尝试了两天将这个结果放入我的 spring 启动项目中。尝试命名本机查询、存储过程和方法签名模式。但每次尝试都失败了。最后,我不得不使用我的 git 客户端放弃更改。
我想要实现的是按部分日期分组,将结果映射到自定义 Pojo 或获取行 MySQL 结果到 spring 应用程序。
请帮助我。
-------------------2017-06-07--------------
问题解决
我设法通过混合MySQL来达到要求JPQL 中的函数DATE_FORMAT(xxx,'%Y-%m')我真的没有任何混合两者的想法,但它有效。我的工作弹簧功能和查询:
@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);
Run Code Online (Sandbox Code Playgroud)
POJO 映射结果:
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class MonthlyScheduleAdherence {
String month;
long actualTotal;
long plannedTotal;
long adherence;
}
Run Code Online (Sandbox Code Playgroud)
我得到:
[
{
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
}
]
Run Code Online (Sandbox Code Playgroud)
这就是我想要的。我设法用这些数据成功制作了这张图表。
谢谢
JPQL 中提供了 Sql 函数。我的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);
Run Code Online (Sandbox Code Playgroud)
查询的结果是以格式化的形式记录按天分组的记录数的列表。
结果行示例:
count: 3, date: 2019, 09, 10
count: 1, date: 2019, 09, 11
Run Code Online (Sandbox Code Playgroud)
JPQL中的简单函数查询:
@Query(value = "SELECT function('date_format', s.date, '%Y, %m, %d') as date from Transactions s;")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6012 次 |
| 最近记录: |