0

I have table already have id and it's unique.

id* status other column ......  amount 
1    1                            100
2    1                            1000
3    1                            1234
4    1                            3000
5    2                            4000
6    1                            200
7    1                            1000
8    1                            2000
9    1                            3000
10   2                            3210

Is there possible to make amount in range as "id" and it's auto increment after select status === 1 and group by amount only in this table?

I'm using sequelize and here is what i tried, i only know how to count the amount in range but don't know how to make it as "id" and all the count result as "count".

x.find({
        where: { status: 1},
        attributes: [
            [literal('COUNT (CASE WHEN amount >= 100 AND amount <= 1000 THEN amount END)'), 'amount 100~1000'],
            [literal('COUNT (CASE WHEN amount >= 1001 AND amount <= 2000 THEN amount END)'), 'amount 1001~2000'],
            [literal('COUNT (CASE WHEN amount >= 2001 AND amount <= 3000 THEN amount END)'), 'amount 2001~3000'],
            [literal('COUNT (CASE WHEN amount >= 3001 THEN amount END)'), 'amount 3001~4000'],
        ],

    });

current result:

{
        "amount 100~1000": 2,
        "amount 1001~2000": 4,
        "amount 2001~3000": 1,
        "amount 3001~4000": 2,
}

expect to get:

{
    "id":1, // amount 100~1000
    "count": 2,
},
{
    "id":2, // amount 1001~2000
    "count": 4,
},
{
    "id":3, // amount 2001~3000
    "count": 1,
},
.
.
.

0 Answers0