Skip to content

use group by and order by together #114

Closed

Description

@huangyingjie

hello,

Here is an example:

select avg(weight) as w , age from person group by age order by w desc;

I have get the result by using 'group by',and next what about to use 'order by' together?

And more complex,adding "where" after "from":

select avg(weight) as w , age from person where country='US' group by age order by w desc;

Activity

  1. dresende commented on Apr 11, 2013

    @dresende
    Owner

    I think this is possible:

    Person.aggregate([ "age" ], { country: "US" }).avg("weight").groupBy("age").get(function (err, groups) {
        // groups should have "age" and "avg_weight"
    });

    The only thing still missing is the order.

  2. dresende commented on Apr 11, 2013

    @dresende
    Owner

    One thing I will probably allow is to define additional properties after the .aggregate() so the syntax gets more clear to read.

  3. huangyingjie commented on Apr 11, 2013

    @huangyingjie
    Author

    Good! Maybe I shall try to read the source code. I will expect the order.

  4. dresende commented on Apr 11, 2013

    @dresende
    Owner

    Coming right out! Just doing the unit tests.

  5. dresende commented on Apr 11, 2013

    @dresende
    Owner

    Now this is possible:

    Person.aggregate({ country: "US" })
          .select("age") // the previous syntax is still available; to pass more properties just pass an Array or each one as a different argument
          .avg("weight")
          .groupBy("age")
          .order("name", "Z")
    .get(function (err, groups) {
        // groups should have "age" and "avg_weight"
    });
  6. huangyingjie commented on Apr 11, 2013

    @huangyingjie
    Author

    so efficient.

  7. huangyingjie commented on Apr 11, 2013

    @huangyingjie
    Author

    I'm worrying about that, '.order("name", "Z")' which the 'name' is an existing table column, what about 'order("w", "Z")'? The 'w' is avg("weight") in my example above.

  8. dxg commented on Apr 11, 2013

    @dxg
    Collaborator

    Just something to be aware of: mysql doesn't follow the sql standard and helps you out a bit with invalid queries; any fields which are in the order statement must also be included in the groupBy one.

    This is a very common bug, I've seen it come up in every ORM I've ever used, and the internets is littered with examples of this.

    Here it's not a huge problem since you can just add .select('name') to the query and it'll work, but I imagine someone will open an issue over this sooner or later.

  9. dresende commented on Apr 11, 2013

    @dresende
    Owner

    Thank you for pointing that out. I'll keep that in mind when changing #115 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions