{"record":{"id":"2e16b383bfa11bd0","repo":"hibernate/hibernate-orm","slug":"within-group-or-over-clause-is-mandatory-for-o","errorCode":null,"errorMessage":"'WITHIN GROUP' or 'OVER' clause is mandatory for ordered set aggregate function: {functionName}","messagePattern":"'WITHIN GROUP' or 'OVER' clause is mandatory for ordered set aggregate function: (.+?)","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":4734,"sourceCode":"\t\t\t}\n\t\t\tif ( ctx.overClause() != null && functionKind == FunctionKind.NORMAL ) {\n\t\t\t\tthrow new SemanticException( \"'OVER' clause is illegal for non-aggregate function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\t\t\tif ( ctx.withinGroupClause() != null && functionKind == FunctionKind.NORMAL ) {\n\t\t\t\tthrow new SemanticException( \"'WITHIN' GROUP clause is illegal for non-aggregate function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\t\t\tif ( ctx.overClause() == null && functionKind == FunctionKind.WINDOW ) {\n\t\t\t\tthrow new SemanticException( \"'OVER' clause is mandatory for window-only function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\t\t\tif ( ctx.withinGroupClause() == null && ctx.overClause() == null\n\t\t\t\t\t&& functionKind == FunctionKind.ORDERED_SET_AGGREGATE ) {\n\t\t\t\tthrow new SemanticException( \"'WITHIN GROUP' or 'OVER' clause is mandatory for ordered set aggregate function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\n\t\t\tif ( ctx.nullsClause() != null ) {\n\t\t\t\tswitch ( functionName ) {\n\t\t\t\t\tcase \"lag\":\n\t\t\t\t\tcase \"lead\":\n\t\t\t\t\tcase \"first_value\":\n\t\t\t\t\tcase \"last_value\":\n\t\t\t\t\tcase \"nth_value\":\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new SemanticException( \"'RESPECT NULLS' or 'IGNORE NULLS' are illegal for function: \"\n\t\t\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\t\t\tquery );\n\t\t\t\t}\n\t\t\t}","sourceCodeStart":4716,"sourceCodeEnd":4752,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L4716-L4752","documentation":"Error \"'WITHIN GROUP' or 'OVER' clause is mandatory for ordered set aggregate function: {functionName}\" thrown in hibernate/hibernate-orm.","triggerScenarios":"An aggregate/window clause is applied to a function that does not accept it: 'WITHIN GROUP' or 'OVER' clause is mandatory for ordered set aggregate function: <value>","commonSituations":"HQL queries that attach FILTER, OVER, WITHIN GROUP, RESPECT/IGNORE NULLS, or FROM FIRST/LAST to functions that are not aggregate or window functions, or omit a mandatory OVER/WITHIN GROUP clause.","solutions":["Ordered-set aggregate functions require WITHIN GROUP or OVER. Add the appropriate clause to the function call."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}