When using the wonderful LIST aggregate with a rather complex expression, how can I specify the order within the list?
I want to have the list ordered by the complex expression itself but would not want to repeat the whole term.
would be sufficient, but unfortunately, order-by ordinal (here: 1) is not allowed for list. Neither is it allowed to give the complex expression an alias and use that as order-by expression. As to the docs, I seem to be allowed to use an integral variable ("order by MyInt") but as I'm using a view, that possibility seems to fail, too. Repeating the whole expression does work, but I would like to avoid that.
Any hints for a "shorter syntax" are highly appreciated:) - I'm using SA 18.104.22.1687.
If no such possibility exists, I would suggest to add that in future version - cf. Breck's answer.
I vote "yes" for an "ORDER BY SELF" enhancement, or ORDER BY 1, or simply ORDERED.
The examples in the Help, such as SELECT LIST( EmployeeID ORDER BY Surname ), simply do not exist in the real world... I cannot imagine ever needing to see '1013,191,750,921,868,1658,...'
Yes, I know what the Help is trying to show, and yes, I often use ORDER BY other-columns-not-in-the-list-expression, but in the simple cases ORDER BY SELF is what is needed: SELECT LIST( EmployeeID ORDER BY EmployeeID )
The Help DOES seem to imply that SELECT LIST( Street ) FROM Employees without an ORDER BY is implicitly ordered by Street: '487 Kennedy Court, 547 School Street' which is in fact just an accident.
answered 14 Oct '10, 14:16
You could wrap the complex expression into a derived table.
A quick little example with the demo database:
Edit: This could also be made into a function of its own...
Resulting in a fairly friendly syntax:
And depending on when you expect this list to change, you could make the function deterministic, so the result may end up being cached.