ORDER BY Syntax
Basic Sorting
Multiple Columns
SORT BY Alias
TeeBI acceptssort by as an alias for order by:
Sort Directions
ascorascending- Sort in ascending order (A-Z, 0-9)descordescending- Sort in descending order (Z-A, 9-0)
TSortItems Class
Programmatic sorting using the TSortItems structure.Structure
Using TSortItems
TBIQuery SortBy
The TBIQuery component provides a SortBy collection. See BI.Query.pas:318.TQuerySortItem Properties
Data- The TDataItem to sort byAscending- True for ascending, False for descending (default: True)IgnoreTextCase- Case-insensitive text sorting (default: True)Enabled- Whether this sort item is active (default: True)
ParseSort Method
Parse ORDER BY strings into TSortItems. See BI.SQL.pas:70.Example
Case Sensitivity
Control case-sensitive sorting for text fields:IgnoreTextCase is True:
- “Apple” and “apple” are treated the same
- Sorting: “Apple”, “Banana”, “cherry”
IgnoreTextCase is False:
- “Apple” comes before “apple”
- Sorting: “Apple”, “Banana”, “apple”, “cherry”
Sorting with Expressions
Sort by calculated expressions:Sorting Grouped Data
Sort aggregated results:- Group dimensions (Country)
- Aggregate measures (sum(Sales))
Multiple Sort Keys
When sorting by multiple columns, earlier columns take precedence:- First by Category (ascending)
- Then by Price (descending) within each Category
Active/Inactive Sort Items
Toggle sort items without removing them:Exchange Sort Items
Change sort priority by exchanging items:Performance Considerations
Sorting Large Datasets
For large datasets, sorting can be expensive. Consider:- Filter first - Reduce data size before sorting
- Limit results - Use TOP/OFFSET to limit sorted rows
- Index usage - Pre-sorted data is faster
Sort Algorithms
TeeBI uses optimized sorting algorithms for different data types:- Integer data: Quicksort
- Float data: Quicksort with NaN handling
- Text data: Case-sensitive or case-insensitive comparison
- DateTime data: Numeric comparison
