{"action":"created","issue":{"url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye/issues/153","repository_url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye","labels_url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye/issues/153/labels{/name}","comments_url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye/issues/153/comments","events_url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye/issues/153/events","html_url":"https://github.com/tomjaguarpaw/haskell-opaleye/issues/153","id":148641493,"number":153,"title":"Implement ordering within aggregations","user":{"login":"hesselink","id":75477,"avatar_url":"https://avatars.githubusercontent.com/u/75477?v=3","gravatar_id":"","url":"https://api.github.com/users/hesselink","html_url":"https://github.com/hesselink","followers_url":"https://api.github.com/users/hesselink/followers","following_url":"https://api.github.com/users/hesselink/following{/other_user}","gists_url":"https://api.github.com/users/hesselink/gists{/gist_id}","starred_url":"https://api.github.com/users/hesselink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hesselink/subscriptions","organizations_url":"https://api.github.com/users/hesselink/orgs","repos_url":"https://api.github.com/users/hesselink/repos","events_url":"https://api.github.com/users/hesselink/events{/privacy}","received_events_url":"https://api.github.com/users/hesselink/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":42,"created_at":"2016-04-15T11:54:39Z","updated_at":"2016-04-18T13:40:05Z","closed_at":null,"body":"I'm trying to tackle ordering within aggregations. This is relevant for some aggregations like `arrayAgg` and `stringAgg`. The [postgres documentation](http://www.postgresql.org/docs/9.3/static/functions-aggregate.html) states that it is \"usually\" enough to just aggregate an ordered subquery to get the result you want. However, I just ran into a case where it isn't, and it depends on the data, not the query. Additionally, there's no clear reason visible to me: adding one record messes up the ordering of another. So I've been looking into what postgres recommends as the fail-safe solution: ordering within aggregations.\r\n\r\nThe SQL syntax for this is something like `array_agg(col_name ORDER BY other_col DESC, third_col ASC)`. I added this to the `PrimQuery` and `Sql` types without issue. However, I run into problems near the surface syntax.\r\n\r\nInitially I considered adding something like `orderAggregate :: Order ? -> Aggregator (Column a) (Column b) -> Aggregator (Column a) (Column b)`. However, the type variable at the question mark is the problem. Just allowing it to be `Column a` obviously isn't enough, since you usually want to order by a different column.\r\n\r\nI've now got something with the signature `orderAggregate :: Order c -> Aggregator c d -> Aggregator c d`. This means that you can use all columns, however, the order then applies to all aggregation operations in the `Aggregator`. I've got this working, and indeed that's what it generates.\r\n\r\nDo you have any ideas how I could have this work per aggregation, but still have access to all the columns to use for ordering?"},"comment":{"url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye/issues/comments/211382537","html_url":"https://github.com/tomjaguarpaw/haskell-opaleye/issues/153#issuecomment-211382537","issue_url":"https://api.github.com/repos/tomjaguarpaw/haskell-opaleye/issues/153","id":211382537,"user":{"login":"tomjaguarpaw","id":1951567,"avatar_url":"https://avatars.githubusercontent.com/u/1951567?v=3","gravatar_id":"","url":"https://api.github.com/users/tomjaguarpaw","html_url":"https://github.com/tomjaguarpaw","followers_url":"https://api.github.com/users/tomjaguarpaw/followers","following_url":"https://api.github.com/users/tomjaguarpaw/following{/other_user}","gists_url":"https://api.github.com/users/tomjaguarpaw/gists{/gist_id}","starred_url":"https://api.github.com/users/tomjaguarpaw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomjaguarpaw/subscriptions","organizations_url":"https://api.github.com/users/tomjaguarpaw/orgs","repos_url":"https://api.github.com/users/tomjaguarpaw/repos","events_url":"https://api.github.com/users/tomjaguarpaw/events{/privacy}","received_events_url":"https://api.github.com/users/tomjaguarpaw/received_events","type":"User","site_admin":false},"created_at":"2016-04-18T13:40:05Z","updated_at":"2016-04-18T13:40:05Z","body":"I am trying to be *incredibly* cautious with the denotational semantics of Opaleye.  Being able to \"order\" an `Aggregator` complicates its meaning considerably."}}