选择带有H2数据库的案例要求按列分组

问题描述 投票:0回答:1

当我使用HQL查询数据库时,此请求可以正常工作并返回良好的数据:

    select
    case 
        when activityjp0_.status<>'CLOSED' 
        and (activityjp0_.user_id is null) then 'UNASSIGNED' 
        else case 
            when activityjp0_.status<>'CLOSED' 
            and activityjp0_.deadline<CURRENT_DATE then 'OVERDUE' 
            else case 
                when activityjp0_.status='PENDING' then 'IN_PROGRESS' 
                else activityjp0_.status 
            end 
        end 
    end as col_0_0_,
    count(distinct activityjp0_.id) as col_1_0_ 
from
    activities activityjp0_ 
group by
    case 
        when activityjp0_.status<>'CLOSED' 
        and (activityjp0_.user_id is null) then 'UNASSIGNED' 
        else case 
            when activityjp0_.status<>'CLOSED' 
            and activityjp0_.deadline<CURRENT_DATE then 'OVERDUE' 
            else case 
                when activityjp0_.status='PENDING' then 'IN_PROGRESS' 
                else activityjp0_.status 
            end 
        end 
    end

当我使用Criteria API生成SAME请求时,它给出了:

    select
    case 
        when activityjp0_.status<>? 
        and (activityjp0_.user_id is null) then 'UNASSIGNED' 
        else case 
            when activityjp0_.status<>? 
            and activityjp0_.deadline<? then 'OVERDUE' 
            else case 
                when activityjp0_.status=? then 'IN_PROGRESS' 
                else activityjp0_.status 
            end 
        end 
    end as col_0_0_,
    count(distinct activityjp0_.id) as col_1_0_ 
from
    activities activityjp0_ 
group by
    case 
        when activityjp0_.status<>? 
        and (activityjp0_.user_id is null) then 'UNASSIGNED' 
        else case 
            when activityjp0_.status<>? 
            and activityjp0_.deadline<? then 'OVERDUE' 
            else case 
                when activityjp0_.status=? then 'IN_PROGRESS' 
                else activityjp0_.status 
            end 
        end 
    end

但是这会导致JDBC错误声明:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "ACTIVITYJP0_.USER_ID" must be in the GROUP BY list; SQL statement:

选择activityjp0_.status <>时的情况?和(activityjp0_.user_id为null),则为'UNASSIGNED',否则当activityjp0_.status <>吗?和activityjp0_.deadline?和(activityjp0_.user_id为null),则为'UNASSIGNED',否则当activityjp0_.status <>吗?和活动jp0_.deadline

如果我替换同一生成的查询中的变量,并在我的数据库上运行它(在h2控制台中,则它没有错误运行)

这怎么可能?

sql jdbc h2 select-case
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.