[英]JPQL greatest-n-per-group query
Is there a way to write this query using JPQL: 有没有一种方法可以使用JPQL编写此查询:
select *
from "tbCita" c inner join "tbAccionCita" ac1
on c."citaId" = ac1."citaId"
left outer join "tbAccionCita" ac2 on ( c."citaId" = ac2."citaId" and
(ac1."fechaAccionCita" < ac2."fechaAccionCita"
or ac1."fechaAccionCita" = ac1."fechaAccionCita"
and ac1."accionCitaId" < ac2."accionCitaId")
)
inner join "tbUsuarioCita" uc
on c."citaId" = uc."citaId"
inner join "tbUser" u
on uc."userId" = u."userId"
where
ac2."accionCitaId" is null
and u."userId" = 1
fechaAccionCita is a date :) fechaAccionCita是一个日期:)
I'm trying to get the latest state (one appointment has multiple states) for all the appointments (tbCita) with userId = 1. 我正在尝试为userId = 1的所有约会(tbCita)获取最新状态(一个约会具有多个状态)。
The query is working as expected, but in JPQL it gives me the following error: 该查询工作正常,但是在JPQL中,它给了我以下错误:
with-clause referenced two different from-clause elements
从句引用了两个不同的从句元素
Here the query: 这里查询:
SELECT c "
+ "FROM Cita c "
+ "INNER JOIN c.usuarioCita u "
+ "INNER JOIN FETCH c.accionCita a "
+ "LEFT OUTER JOIN c.accionCita a2 on (a.fechaAccionCita < a2.fechaAccionCita or "
+ "a.fechaAccionCita = a2.fechaAccionCita and a.accionCitaId < a2.accionCitaId) "
Does the following work? 请问以下工作吗? Also, are you sure the logic in the OR statement working as expected?
另外,您确定OR语句中的逻辑按预期工作吗? It seems you might need an additional set of parentheses.
似乎您可能需要附加一组括号。
select c
from tbCita c, tbAccionCita ac1, tbUsuarioCita uc, tbUser u
left join tbAccionCita ac2 on (
c.citaId = ac2.citaId
and (ac1.fechaAccionCita < ac2.fechaAccionCita
or ac1.fechaAccionCita = ac1.fechaAccionCita
and ac1.accionCitaId < ac2.accionCitaId))
where c.citaId = ac1.citaId
and c.citaId = uc.citaId
and uc.userId = u.userId
and ac2.accionCitaId is null
and u.userId = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.