我想通过JPASQLQuery API设置一个SQL case表达式,如下所示:
Oracle SQL是:
SELECT h.ordercode
, h.requesteddeliverytime
, CASE
WHEN h.ordergroup IS NOT NULL
THEN g.minRequestedDeliveryTime
ELSE h.requesteddeliverytime
END AS earliest_del_time
FROM orderheader h LEFT JOIN (
SELECT
ordergroup
,MIN(requesteddeliverytime) as minRequestedDeliveryTime
FROM orderheader hh
GROUP BY orderGroup
) g ON h.ordergroup = g.ordergroup
并提供正确的结果。当我尝试设置CaseBuilder()时,我得到一个简单的Expression
,然后再不能在JPASQLQuery中使用(至少我无法使用.as("someString")
设置别名)。
元数据类如下所示(简短形式):
@Generated("com.mysema.query.sql.codegen.MetaDataSerializer")
public class SOrderheader extends com.mysema.query.sql.RelationalPathBase<SOrderheader> {
public final StringPath ordergroup = createString("ORDERGROUP");
public final DateTimePath<java.sql.Timestamp> requesteddeliverytime = createDateTime("REQUESTEDDELIVERYTIME", java.sql.Timestamp.class);
}
,组查询如下所示:
ListSubQuery<Tuple> groupSubQuery = new SQLSubQuery()
.from(sOrderGroup)
.groupBy(sOrderGroup.ordergroup)
.list(new QTuple(
sOrderGroup.ordergroup.as("orderGroup"),
sOrderGroup.id.count().as("nofOrdersPerGroup"),
sOrderGroup.requesteddeliverytime.min().as("earliestDeliveryTime"),
));
CaseBuilder()声明:
Expression<Timestamp> earliestDeliveryTime = new CaseBuilder()
.when(sOrderHeader.ordergroup.isNotNull()).then(og.getDate("earliestDeliveryTime", Date.class))
.otherwise(sOrderHeader.requesteddeliverytime);
我希望有一些方法可以在各种CASE替代方法中返回不同的列和/或为从CaseBuilder()返回的表达式设置别名的可能性。任何帮助表示赞赏。