{"record":{"id":"5e0d84519f184a38","repo":"hibernate/hibernate-orm","slug":"position-based-order-by-is-not-allowed-in-over","errorCode":null,"errorMessage":"Position based 'order by' is not allowed in 'over' or 'within group' clauses","messagePattern":"Position based 'order by' is not allowed in 'over' or 'within group' clauses","errorType":"exception","errorClass":"SyntaxException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":1626,"sourceCode":"\t\treturn expressions;\n\t}\n\n\tprivate SqmExpression<?> resolveOrderByOrGroupByExpression(\n\t\t\tParseTree child,\n\t\t\tboolean definedCollate,\n\t\t\tboolean allowPositionalOrAliases) {\n\t\tfinal var nodeBuilder = nodeBuilder();\n\t\tfinal var processingState = processingStateStack.getCurrent();\n\t\tfinal var processingQuery = processingState.getProcessingQuery();\n\t\tfinal var queryPart = sqmQueryPart( processingQuery );\n\t\tif ( child instanceof TerminalNode ) {\n\t\t\tif ( definedCollate ) {\n\t\t\t\t// This is syntactically disallowed\n\t\t\t\tthrow new SyntaxException( \"'collate' is not allowed for position based 'order by' or 'group by' items\" );\n\t\t\t}\n\t\t\telse if ( !allowPositionalOrAliases ) {\n\t\t\t\t// This is syntactically disallowed\n\t\t\t\tthrow new SyntaxException( \"Position based 'order by' is not allowed in 'over' or 'within group' clauses\" );\n\t\t\t}\n\n\t\t\tfinal int position = Integer.parseInt( child.getText() );\n\n\t\t\t// make sure this selection exists\n\t\t\tfinal var nodeByPosition = nodeByPosition( queryPart, position, processingState );\n\t\t\tif ( nodeByPosition == null ) {\n\t\t\t\tthrow new SemanticException( \"Numeric literal '\" + position\n\t\t\t\t\t\t+ \"' used in 'group by' does not match a registered select item\",\n\t\t\t\t\t\tquery );\n\t\t\t}\n\n\t\t\treturn new SqmAliasedNodeRef(\n\t\t\t\t\tposition,\n\t\t\t\t\tnodeBuilder.resolveExpressible( integerDomainType ),\n\t\t\t\t\tnodeBuilder\n\t\t\t);\n\t\t}","sourceCodeStart":1608,"sourceCodeEnd":1644,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L1608-L1644","documentation":"Error \"Position based 'order by' is not allowed in 'over' or 'within group' clauses\" thrown in hibernate/hibernate-orm.","triggerScenarios":"The HQL/JPQL query violates a structural language rule.","commonSituations":"Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.","solutions":["Use expression-based ordering inside 'over'/'within group' clauses.","Remove the positional reference."],"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"}