Sql 如何根据JPA中的group by筛选出条件?
我有一张像这样的桌子Sql 如何根据JPA中的group by筛选出条件?,sql,jpa,jpql,Sql,Jpa,Jpql,我有一张像这样的桌子 | customer | profile | status | date | | 1 | 1 | DONE | mmddyy | | 1 | 1 | DONE | mmddyy | 在这种情况下,我希望根据具有max date的配置文件ID进行分组。配置文件可以重复。我排除了Java8流,因为这里有很多条件 我想将以下SQL转换为JPQL: select customer, profile, st
| customer | profile | status | date |
| 1 | 1 | DONE | mmddyy |
| 1 | 1 | DONE | mmddyy |
在这种情况下,我希望根据具有max date的配置文件ID进行分组。配置文件可以重复。我排除了Java8流,因为这里有很多条件
我想将以下SQL转换为JPQL:
select customer, profile, status, max(date)
from tbl
group by profile, customer,status, date, column-k
having count(profile)>0 and status='DONE';
如果这个查询在SQL中是正确的,有人能告诉我如何用JPQL编写它吗?如果我在
select
中声明列,则在group by中也需要该列,并且查询结果不同。在查询中日期列在group by中不需要,并且status='DONE'
应添加where子句
select customer, profile, status, max(date)
from tbl
where status='DONE'
group by profile, customer,status,
having count(profile)>0
我猜您需要完成的最新客户/配置文件组合
如果是,正确的SQL是:
select t.*
from t
where t.date = (select max(t2.date)
from t t2
where t2.customer = t.customer and t2.profile = t.profile
) and
t.status = 'DONE';
我不知道如何将其转换为JPQL,但您最好从使用SQL代码开始。谢谢您,这很管用!我假设这不能用一个groupby和一个简单的子句来完成。