Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize query to subtract value from previous row value

I'm using PostgreSQL. I have a reading value in my table. From that, I have to find the consumption by subtracting previous value.

I found the SQL query. How to use that in sequelize? Is there any other option except raw queries?

SQL Query:

SELECT "readingValue",
       "readingValue" - COALESCE(LAG("readingValue") OVER
           (ORDER BY "readingTime")) AS consumption
FROM public."tableName" LIMIT 100;
like image 268
Jegan Avatar asked Nov 19 '25 10:11

Jegan


1 Answers

You can do that with sequelize literal.

model.update({'count': sequelize.literal('count + 1')}, { where: { id: 1 }})

You can also do it with increment method.

Model.increment('seq', { by: 5, where: { id: 'model_id' });

like image 57
Siddharth Sunchu Avatar answered Nov 21 '25 10:11

Siddharth Sunchu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!