sequelize / sequelize/sequelize

Sequelize to handle WITH ROLLUP group by's (MySQL)

Open
#6,063 11 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Hi good folks,

Let me introduce the ROLLUP feature from MySQL 5.5+ with examples:

Simple GROUP BY sum:

mysql> SELECT year, SUM(profit) FROM sales GROUP BY year;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 |        4525 |
| 2001 |        3010 |
+------+-------------+

Adding a WITH ROLLUP modifier to the GROUP BY clause causes the query to produce another row that shows the grand total over all (grouped up) values

mysql> SELECT year, SUM(profit) FROM sales GROUP BY year WITH ROLLUP;
+------+-------------+
| year | SUM(profit) |
+------+-------------+
| 2000 |        4525 |
| 2001 |        3010 |
| NULL |        7535 |
+------+-------------+

http://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html

So, this is a nice featurette that I think would be fun for reports. But, I wasn't able to use on Sequelize because it is not recognized (not even cited on Docs) and so, even forcing it to include that clause after the last group item with Sequelize.literal it was not included on group arrays.

I think it could be set as (regarding the above example):

Sales.findAll({
  attributes: ['year', [Sequelize.fn('SUM', Sequelize.col('profit')), 'profitSum'],
  group: ['year'],
  rollup: true
})

Resulting in an new array item as:

[
  {year: 2000, profitSum: 4525},
  {year: 2001, profitSum: 3010},
  {year: null, profitSum: 7535, summary: true}
]

Rollup also replaces ORDER BY clause, so it would be excludent with order param.

What do you think of that?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the Sales.findAll options shown and trace how group arrays and Sequelize.literal are handled for MySQL. The proposed rollup option, interaction with order, and summary field need agreement before implementation. Done would require a settled API and tests covering the generated query and returned rows.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.