按日期对小组进行分组,忽略小时/分钟/秒

时间:2016-01-28 23:10:33

标签: javascript sql node.js postgresql sequelize.js

嘿所以我试图从数据库中查询,使用Sequelize(Node.js ORM for postgreSQL),我试图按日期范围分组,并保持该表中多少项的计数。

现在我的代码是

 Task.findAll({
    attributes: ['createdAt'],
    group: 'createdAt'
  })

但是你可以看到分组只考虑确切的日期(包括秒),所以分组实际上是没有意义的,因为无论什么都没有重叠的项目与完全相同的第二个计数。 所以我希望它只是基于日,年和月的小组。

我假设它必须像sequelize.fn(...)

4 个答案:

答案 0 :(得分:12)

正如你所说,它是用#include <Servo.h> const int servo1 = 11; // first servo const int servo2 = 10; // second servo const int joy1 = 5; // switch 1 const int joy2 = 4; // switch 2 const int joy3 = 3; // switch 3 const int joy4 = 2; // switch 4 int servoVal; // variable to read the value from the digital pin Servo myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo void setup() { // Servo myservo1.attach(servo1); // attaches the servo myservo2.attach(servo2); // attaches the servo // Inizialize Serial Serial.begin(9600); } void loop(){ // Display Joystick values using the serial monitor outputJoystick(); // Read the horizontal joystick value (value between 0 and 180) servoVal = digitalRead(joy1, joy2, joy3, joy4); servoVal = map(servoVal, 0, 45, 135, 180); // scale it to use it with the servo (result between 0 and 180) myservo2.write(servoVal); // sets the servo position according to the scaled value // Read the horizontal joystick value (value between 0 and 180) servoVal = digitalRead(joy1, joy2, joy3, joy4); servoVal = map(servoVal, 0, 45, 135, 180); // scale it to use it with the servo (result between 0 and 180) myservo1.write(servoVal); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } /** * Display joystick values */ void outputJoystick(){ Serial.print(digitalRead(joy1, joy2, joy3, joy4)); Serial.print ("---"); Serial.print(digitalRead(joy1, joy2, joy3, joy4)); Serial.println ("----------------"); } 完成的,没有别的办法。尝试:

sequelize.fn(...)

我认为这可能会起作用。如果没有,我们会看到如何做到这一点;)

请注意,PostgreSQL允许您截断到特定的时间间隔。有关更多信息,请访问:http://www.postgresql.org/docs/9.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

另外,要了解群组(和订单)的工作原理,请参阅Sequelize的文档:https://github.com/sequelize/sequelize/blob/172272c8be9a847b2d64f0158826738703befddf/docs/docs/models-usage.md#manipulating-the-dataset-with-limit-offset-order-and-group

答案 1 :(得分:1)

所选答案在这里无效。

这就是对我有用的东西。

Task.findAll({
    attributes: [
        [Sequelize.literal(`DATE("createdAt")`), 'date'],
        [Sequelize.literal(`COUNT(*)`), 'count']
    ],
    group: ['date'],
})

答案 2 :(得分:0)

您也可以尝试:

     group:'DATE(date_added)' 
     or
     group:'WEEK(date_added)'
     or
     group:'MONTH(date_added)'

虽然你必须设置sql_mode =&#34;&#34;因为你否则会收到ONLY_FULL_GROUP_BY错误。

答案 3 :(得分:0)

对于 Sequelize 和 MYSQL

这对我有用:

Model.findAll({
      attributes: [
        /* add other attributes you may need from your table */
        [sequelize.fn('DATE', sequelize.col('createdAt')), 'Date']
      ],
      group: [sequelize.fn('DATE', sequelize.col('createdAt')), 'Date']
    })