Skip to content

Join and sum #2427

Closed
Closed

Description

@tom95

I was not able to find a way to run a sum on an include. The special use case here would be a posts table with a separate votings table from which I need only the sum of upvotes/downvotes. If that's already possible without running raw queries, it would probably be worth adding to the wiki.

Activity

  1. mickhansen commented on Oct 17, 2014

    @mickhansen
    Contributor

    Can you show the include you are using?
    You can change the attributes loaded with something like:

    attributes: [
      [sequelize.fn('SUM', sequelize.col('column')), 'sumOfColumn']
    ]
  2. dongseok0 commented on May 26, 2015

    @dongseok0

    I have same question like below :
    It seems include is not supported by sum method?

        Model.sum('point', {
                where: {
                    verified: false
                },
                include: [{
                    model: Model2,
                    where: {
                        id: this.params.id
                    }
                }]
            });
    

    Anyways, I managed to get same result by using your attributes example, but it would be nice include supported by aggregate or sum method.

  3. janmeier commented on May 26, 2015

    @janmeier
    Member

    @dongseok0 Can you provide some more context? What do your models and associations look like, and which column do you want to sum on?

  4. dongseok0 commented on May 28, 2015

    @dongseok0

    Association:

    Model.belongTo(Model2);
    Model2.hasMany(Model);
    

    And I want the 'point' column from Model. Currently I'm doing below:

    Model.findOne({
                attributes: [
               [sequelize.fn('SUM', sequelize.col('point')), 'points']
            ],
                where: {
                    verified: false
                },
                include: [{
                    model: Model2,
                    where: {
                        id: this.params.id
                    }
                }]
            });
    
  5. pimterry commented on Jun 24, 2015

    @pimterry
    Contributor

    Am I missing something @dongseok0? The example you give doesn't work for me, I get "'Column 'Model.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause".

    As soon as I add an 'include' array, Sequelize automatically Model.id to the SELECT (as noted in #3920, because it's the primary key) and this breaks the aggregation. See

    if (options.include) {
    . Are you sure this works? Any idea what you're doing that I'm not?

  6. dongseok0 commented on Jun 25, 2015

    @dongseok0

    @pimterry - Yeah, above example is currently working on our production.
    Do you have any problems if you just find instead of using fn?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions