帮助将SQL查询转换为JPQL

时间:2010-08-04 14:17:58

标签: sql jpql

我有一个SQL查询需要转换为JPQL,我开始怀疑这是否是我必须使用本机SQL的情况之一。作为参考,这是SQL查询。

SELECT c.title, a.approval_total, r.requested_total
FROM
   codes c
INNER JOIN
 (SELECT code_id, year, SUM(requested_amount) requestedTotal
   FROM requests
   GROUP BY code_id, year) r
 ON c.id = r.code_id
INNER JOIN
 (SELECT code_id, year, SUM(approved_amount) approvedTotal
   FROM approvals
   GROUP BY code_id, year) a
 ON c.id = a.code_id
WHERE c.title = ? AND r.fiscal_year = ? and a.fiscal_year = ?

有没有人知道将这两个内联视图翻译成JPQL的方法?或者,可以采用不同的方式构建查询,以便更容易翻译?

1 个答案:

答案 0 :(得分:0)

我首先会在不使用内联选择

的情况下重写SQL查询

不确定我是否写得正确,但它会是这样的:

SELECT c.title, sum(a.approved_amount) as approval_total, SUM(r.requested_amount) as requested_total
FROM codes c
INNER JOIN requests r ON c.id = r.code_id
INNER JOIN approvals a ON c.id = a.code_id
WHERE c.title = ? AND r.fiscal_year = ? and a.fiscal_year = ?
GROUP By r.code_id, r.year, a.code_id, a.year