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

How do I SUM column in join? #2827

Closed
kwiliodev opened this issue Jan 9, 2024 · 2 comments
Closed

How do I SUM column in join? #2827

kwiliodev opened this issue Jan 9, 2024 · 2 comments

Comments

@kwiliodev
Copy link

I am trying to get a sum of bills for each category. I see the .sum() only seems to work on the selected table but not the joined table.
How can I return a column similar to the 'amountOfBills' count but a sum of the 'amount' column in the related bills?
Thanks.

Stream<List<CategoryWithTypes>> streamCategoryWithTypes() {
    final amountOfBills = bills.id.count();

    // add sum of bills [ ISSUE HERE]
     final sumOfBills = bills.amount.sum();

    final query = select(categories).join([
      leftOuterJoin(types, types.id.equalsExp(categories.typeId)),
      leftOuterJoin(bills, bills.categoryId.equalsExp(categories.id) & bills.deletedAt.isNull()),
    ]);
    // order by category name ascending
    query.orderBy([OrderingTerm.asc(categories.name)]);
    query.addColumns([bills.id.count()]);
    query.groupBy([categories.id]);

    // see next section on how to parse the result
    return query.watch().map((rows) {
      
      return rows.map((row) {
        return CategoryWithTypes(
          row.readTable(categories),
          row.readTable(types),
          row.read(amountOfBills),
          row.read(sumOfBills)
        );
      }).toList();
    });
}
@simolus3
Copy link
Owner

Were you able to resolve the problem? Out of interest, what was the problem with that snippet? It looks correct to me.

@kwiliodev
Copy link
Author

Yes, I was able to resolve. The issue was that my column was a TextColumn. I changed it to RealColumn.
Works great now, thank you!

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

2 participants