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

Cannot select aggregate and non-aggregate fields #772

Closed
jasonl opened this Issue Mar 1, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@jasonl

jasonl commented Mar 1, 2017

With a table

table! {
    mrr_by_date (account_id, date) {
        account_id -> Integer,
        date -> Date,
        mrr -> Double,
    }
}

I cannot do:

mrr_by_date
            .group_by(date)
            .select((sum(mrr), date))
            .filter(account_id.eq(123))

Because I get the following error:

149 |             .select((sum(mrr), date))
    |              ^^^^^^ the trait `diesel::SelectableExpression<models::mrr_by_date::mrr_by_date::table, _>` is not implemented for `(diesel::expression::Sum<models::mrr_by_date::mrr_by_date::columns::mrr>, models::mrr_by_date::mrr_by_date::columns::date)`
    |
    = help: the following implementations were found:
    = help:   <(A, B) as diesel::SelectableExpression<QS, (SA, SB)>>
    = help:   <(A, B) as diesel::SelectableExpression<QS, diesel::types::Nullable<(SA, SB)>>>
    = note: required because of the requirements on the impl of `diesel::query_builder::Query` for `diesel::query_builder::SelectStatement<_, (diesel::expression::Sum<models::mrr_by_date::mrr_by_date::columns::mrr>, models::mrr_by_date::mrr_by_date::columns::date), models::mrr_by_date::mrr_by_date::table, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::NoWhereClause, diesel::query_builder::order_clause::NoOrderClause, diesel::query_builder::limit_clause::NoLimitClause, diesel::query_builder::offset_clause::NoOffsetClause, diesel::query_builder::group_by_clause::GroupByClause<models::mrr_by_date::mrr_by_date::columns::date>>`
    = note: required because of the requirements on the impl of `diesel::SelectDsl<(diesel::expression::Sum<models::mrr_by_date::mrr_by_date::columns::mrr>, models::mrr_by_date::mrr_by_date::columns::date), _>` for `diesel::query_builder::SelectStatement<(diesel::types::Integer, diesel::types::Date, diesel::types::Double), (models::mrr_by_date::mrr_by_date::columns::account_id, models::mrr_by_date::mrr_by_date::columns::date, models::mrr_by_date::mrr_by_date::columns::mrr), models::mrr_by_date::mrr_by_date::table, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::NoWhereClause, diesel::query_builder::order_clause::NoOrderClause, diesel::query_builder::limit_clause::NoLimitClause, diesel::query_builder::offset_clause::NoOffsetClause, diesel::query_builder::group_by_clause::GroupByClause<models::mrr_by_date::mrr_by_date::columns::date>>`

(I have edited the code and error messages slightly to remove what I believe is non-relevant information, am happy to share the full error messages privately).

Am I reading this correctly as there isn't support for combining aggregate and non-aggregate fields in a select statement, despite the SQL intended to be produced being valid:

SELECT sum(mrr), date FROM mrr_by_date WHERE account_id = $1 GROUP BY date

Or am I doing something wrong here?

@sgrif

This comment has been minimized.

Member

sgrif commented Mar 1, 2017

No, you're doing nothing wrong. We don't fully support group by yet. See #210 and #3

@sgrif sgrif closed this Mar 1, 2017

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