execute sql query on sequelize

372 views Asked by At

I have the following SQL query being executed in MySQL. How do I turn this query into a query in Sequelize format.

SELECT * 
FROM   weather_datas AS S 
WHERE  created_at = (SELECT Max(created_at) 
                     FROM   weather_datas 
                     WHERE  control_esp_id = S.control_esp_id 
                     GROUP  BY control_esp_id) 
1

There are 1 answers

2
Aryan On

may be this can get data you wanted

await weather_datas.findAll({
  attributes: [[Sequelize.fn('max', Sequelize.col('created_at')), 'max']],
  group: ['control_esp_id'],
  where : {
   control_esp_id : req.body.control_esp_id}
})