java - Spring JPA Query - 替换结果集中的负值
问题描述
我正在使用 Spring Data JPA 开发一个 Spring Boot 应用程序。我正在使用自定义 JPQL 查询来获取我需要的数据。
以下是我的方法存储库:
/**
* SELECT sku, SUM(real_allocation - logical_allocation)
* FROM stock
* WHERE warehouse_brand = '?'
* GROUP BY (warehouse_brand, sku);
*/
@Query(value = "SELECT new it.reply.retail.oms.common.api.inventory.CumulativeInventoryItem " +
"(s.pk.sku, SUM(s.realAllocation - s.logicalAllocation)) " +
"FROM Stock s WHERE s.pk.whsCode IN :warehouses GROUP BY (s.pk.sku)")
Page<CumulativeInventoryItem> getCumulativeInventory(List<String> warehouses, Pageable pageable);
自CumulativeInventoryItem
定义对象如下:
public class CumulativeInventoryItem {
private String sku;
private Long cumulativeQty;
}
cumulativeQty
我获得的结果集有时包含需要用 0 替换的属性的负值。
是否可以使用临时查询替换这些值,或者一旦我从 JPA 方法中获取结果集后我需要替换它们?
解决方案
据此:https ://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF#New_in_JPA_2.0
CASE WHEN THEN ELSE 组合可以在那里使用。
可以试试吗
SUM(CASE WHEN (s.realAllocation - s.logicalAllocation < 0) THEN 0 ELSE (s.realAllocation - s.logicalAllocation)))
代替
SUM(s.realAllocation - s.logicalAllocation)
推荐阅读
- java - tomcat netbeans org.apache.catalina.core.ApplicationContext.log 上的错误
- android - 找不到动态加载库的基地址或导出函数
- excel - CountIf 起始单元格取决于另一个单元格中输入的内容?
- javascript - Javascript onclick 代理
- laravel - Laravel:如何从 View/Blade 获取数据并传递给控制器
- bash - 将换行变量附加到 shell 脚本中
- r - 更改 openxlsx 工作簿中的单元格值
- forms - Symfony ValidatorComponent > FormComponent 中的 AnnotationMapping
- javascript - Redux:根据 Actions 改变 Reducer 中初始状态的不同部分
- vue.js - 关于vuejs中静态资产的另一个问题