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

ER_BAD_FIELD_ERROR: Unknown column 'distinctAlias.operations_id' in 'field list' #334

Closed
devfelipereis opened this issue Aug 18, 2020 · 1 comment

Comments

@devfelipereis
Copy link

devfelipereis commented Aug 18, 2020

Hello!

I'm trying to make an aggregate query and I'm having this error.

ER_BAD_FIELD_ERROR: Unknown column 'distinctAlias.operations_id' in 'field list'

Of course that operations.id doesn't exists because I'm not selecting it.

Check my query:

const queryBuilder = await Operation.createQueryBuilder('operations');
    queryBuilder.select(
      'DATE_FORMAT(operations.date, "%Y-%m-%d") as date,' +
        'SUM(operations.numberOfContracts) as numberOfContracts, SUM(operations.value) as value,' +
        'wallets.name as walletName, ifnull(SUM(rates.bmf),0) as bmf, ifnull(SUM(rates.brokerage),0) as brokerage,' +
        'ifnull(SUM(rates.emolument),0) as emolument, ifnull(SUM(rates.irrf),0) as irrf, ifnull(SUM(rates.otherCosts),0) as otherCosts',
    );
    queryBuilder.innerJoin('operations.wallet', 'wallet');
    queryBuilder.leftJoin('wallet.rates', 'rates');
    queryBuilder.where('wallet.userId = :userId', { userId: user.id });
    queryBuilder.groupBy('date, operations.walletId');

If I add operations.id as operations_id to the select, then I got this:

QueryFailedError: ER_WRONG_FIELD_WITH_GROUP: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.operations.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

The last error will desapear if I add operations.id to the group by... but, the returned list will be empty.

I don't know what I'm doing wrong, I'm not a sql expert.

Could you help me? thank you!

@devfelipereis
Copy link
Author

I'm doing a messy... I'll try with subqueries.. sorry..

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

No branches or pull requests

1 participant