Aggregate SQL Translation Examples
Internal reference showing how PsoftQL aggregate requests translate to SQL
Categories:
This document shows how PsoftQL aggregate requests translate to SQL queries. This is an internal reference for implementation.
Example 1: Simple COUNT with GROUP BY
PsoftQL Request
Generated SQL
Example 2: COUNT with Criteria (WHERE clause)
PsoftQL Request
Generated SQL
Example 3: Multiple GROUP BY Fields
PsoftQL Request
Generated SQL
Example 4: Multiple Aggregate Functions
PsoftQL Request
Generated SQL
Example 5: No GROUP BY (Total Aggregation)
PsoftQL Request
Generated SQL
Note: No GROUP BY clause, no ORDER BY clause when there are no groupByFields.
Example 6: COUNT DISTINCT
PsoftQL Request
Generated SQL
Example 7: With Effective Date Logic
When noEffectiveDateLogic is false (default) and the record has EFFDT:
PsoftQL Request
Generated SQL (Oracle)
Example 8: With rowLimit (Top N)
PsoftQL Request
Generated SQL (Oracle 12c+)
Generated SQL (SQL Server)
Example 9: With sqlWhereClause
PsoftQL Request
Generated SQL
Example 10: Users per Role (Count from Related Table)
PsoftQL Request
Generated SQL
Implementation Notes
SQL Generation Pattern
Field Prefix Handling
The aggregate function string needs to be parsed to add the A. alias prefix to field names:
| Input | Output |
|---|---|
COUNT(*) |
COUNT(*) (no change) |
COUNT(1) |
COUNT(1) (no change) |
COUNT(EMPLID) |
COUNT(A.EMPLID) |
COUNT(DISTINCT EMPLID) |
COUNT(DISTINCT A.EMPLID) |
SUM(ANNUAL_RT) |
SUM(A.ANNUAL_RT) |
AVG(SALARY) |
AVG(A.SALARY) |
MIN(HIRE_DT) |
MIN(A.HIRE_DT) |
MAX(LASTUPDDTTM) |
MAX(A.LASTUPDDTTM) |
Validation Checks
- If
isAggregate: trueandpageNumber > 1→ Error - If
isAggregate: trueand multiple records withparentRecordName→ Error - If
aggregateConfigmissing on the record whenisAggregate: true→ Error - If
aggregateFieldsempty whenisAggregate: true→ Error - Validate field names in
groupByFieldsexist on the record - Parse and validate field names in
functionexpressions exist on the record (except*and literals like1)