Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support keyword distinct #123

Closed
huangyingjie opened this issue Apr 15, 2013 · 8 comments
Closed

support keyword distinct #123

huangyingjie opened this issue Apr 15, 2013 · 8 comments

Comments

@huangyingjie
Copy link

hello,
Here is another request:

      select distinct country from person;

How to get it?
And Can I execute any sql like hsql in hibernate?

@dresende
Copy link
Owner

There isn't. I started the project as a way for people to completely avoid using sql but now I'm thinking about having that possibility (with a warning about compatibility if switching to other dbms).

@dresende
Copy link
Owner

About the distinct, that's the query you want to do (in your example) or a more complex one? I was thinking about the aggregation functions..

@huangyingjie
Copy link
Author

just the distinct. Using sql while some functions have not been developped.

dresende added a commit that referenced this issue Apr 16, 2013
Usually, when using .avg('age'), ORM will define an alias called
avg_age to the returned value. If you want to change this do
.avg('age').as('my_age').
@dresende
Copy link
Owner

This is probably going to appear a little odd, perhaps we can shorten the code in the future. I added DISTINCT to sql-query dependency and updated ORM. Also, added .as() to add aliases to functions instead of the auto-generated (if needed). Here's an example:

Person.aggregate().distinct('country').as('country').groupBy('country').get(function (err, countries) {
    // groupBy is needed or else it will return only the first one
});

As you can see, .groupBy shouldn't be needed since the query with or without it should return the same thing, but ORM returns only one row for aggregate functions when not using .groupBy.

@dresende
Copy link
Owner

This is because DISTINCT is not actually an aggregate function (I think!) but it works well with them.

@huangyingjie
Copy link
Author

Acturally it is a bit odd, we use it temporarily.

dresende added a commit that referenced this issue Apr 17, 2013
@dresende
Copy link
Owner

You should now be able to do:

Person.aggregate().distinct('country').get(function (err, countries) {
    // countries should be an Array like [ 'country1', 'country2', ... ]
});

@dresende
Copy link
Owner

If you have a more complex query please post it, I'm not sure how it handles it using more functions.

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

No branches or pull requests

2 participants