【发布时间】:2015-05-29 21:37:57
【问题描述】:
我想知道在连接到子查询的 SQL 样式 QueryDSL 中实现此查询的最佳方式。我有点挣扎,但让它生成了必要的 SQL。但是,我想知道是否有任何简化/改进,特别是与我必须创建的三个“路径”相关?例如,可以根据 latestSubQuery 定义 latestCaseId。
在下面我实际查询的简化形式中,我找到了每个案例组具有最新时间戳的记录集(分布在 ucm 和 pcm 中的字段)。子查询标识每个组的最新时间戳,以便我们可以通过它过滤外部查询。
final SimplePath<ListSubQuery> latestSubQueryPath = Expressions.path(ListSubQuery.class, "latest");
final SimplePath<Timestamp> caseLatestMentioned = Expressions.path(Timestamp.class, "caseLatestMentioned");
final SimplePath<Integer> latestCaseId = Expressions.path(Integer.class, "latest.caseId");
final ListSubQuery<Tuple> latest = new SQLSubQuery()
.from(ucm2)
.innerJoin(pcm2).on(ucm2.id.eq(pcm2.id))
.groupBy(pcm2.caseId)
.list(pcm2.caseId.as(latestCaseId), ucm2.lastExtracted.max().as(caseLatestMentioned));
q.from(ucm)
.join(pcm).on(ucm.id.eq(pcm.id))
.innerJoin(latest, latestSubQueryPath).on(pcm.caseId.eq(latestCaseId))
.where(ucm.lastExtracted.eq(caseLatestMentioned));
【问题讨论】: