Search code examples
node.jsapisequelize.jsbackend

get custom query with nodejs sequelize with mysql


if this is my data

name code qnt
t 2 4
t 2 5
b 3 3
b 3 2
b 3 7

I want to get data like this

name code qnt
t 2 4
t 2 5
- - 9
b 3 3
b 3 2
b 3 7
- - 12

I mean to get all rows and total of qnt of rows that have same name and code under rows.

I have to show a table in this format


Solution

  • You can use WITH ROLLUP from MySQL and need to use the raw SQL query:

    const rows = await sequelize.query(`
    SELECT name, code, qnt
    FROM table
    GROUP BY name WITH ROLLUP
    `, {
      type: QueryTypes.SELECT
    });