Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何根据JPA中的group by筛选出条件?_Sql_Jpa_Jpql - Fatal编程技术网

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和一个简单的子句来完成。