我有多个相同的日期,我想对日期进行分组,因此我有一个time
和bg_reading
数组,因为同一日期会有多个值。这就是我的JSON吐出来的。
`[
{
"date": "2016-03-18T00:00:00.000Z",
"time": "22:03:54",
"bg_reading": 9.4
},
{
"date": "2016-03-19T00:00:00.000Z",
"time": "12:05:56",
"bg_reading": 12.4
},
{
"date": "2016-03-20T00:00:00.000Z",
"time": "01:17:06",
"bg_reading": 15.5
},
{
"date": "2016-03-21T00:00:00.000Z",
"time": "06:27:42",
"bg_reading": 17.1
},
{
"date": "2016-03-22T00:00:00.000Z",
"time": "06:19:21",
"bg_reading": 27.6
},
{
"date": "2016-03-22T00:00:00.000Z",
"time": "17:03:27",
"bg_reading": 14.5
},
{
"date": "2016-03-18T00:00:00.000Z",
"time": "22:03:54",
"bg_reading": 9.4
},
{
"date": "2016-03-19T00:00:00.000Z",
"time": "12:05:56",
"bg_reading": 12.4
},
{
"date": "2016-03-20T00:00:00.000Z",
"time": "01:17:06",
"bg_reading": 15.5
},
{
"date": "2016-03-21T00:00:00.000Z",
"time": "06:27:42",
"bg_reading": 17.1
},
{
"date": "2016-03-22T00:00:00.000Z",
"time": "06:19:21",
"bg_reading": 27.6
},
{
"date": "2016-03-22T00:00:00.000Z",
"time": "17:03:27",
"bg_reading": 14.5
},
{
"date": "2016-03-18T00:00:00.000Z",
"time": "22:03:54",
"bg_reading": 9.4
},
{
"date": "2016-03-19T00:00:00.000Z",
"time": "12:05:56",
"bg_reading": 12.4
},
{
"date": "2016-03-20T00:00:00.000Z",
"time": "01:17:06",
"bg_reading": 15.5
},
{
"date": "2016-03-21T00:00:00.000Z",
"time": "06:27:42",
"bg_reading": 17.1
},
{
"date": "2016-03-22T00:00:00.000Z",
"time": "06:19:21",
"bg_reading": 27.6
},
{enter code here
"date": "2016-03-22T00:00:00.000Z",
"time": "17:03:27",
"bg_reading": 14.5
}
]`
我希望能有这样的东西:
`[
{
"date": "2016-03-18T00:00:00.000Z",
"time": ["05:22:33", "09:33:10", "15:01:34", "18:03:34", "21:34:09"],
"bg_reading": [9.4, 6.3, 13.3, 6.8, 7.3]
}...
]`
但是现在我不知道为什么有这么多的重复值以及为什么有多个日期。这就是我试图在Sequelise上查询它的方式。
` Log.findAll({
where: {
bg_reading: {gt: 0}
},
attributes: ['date', 'time', 'bg_reading'],
group: 'date',
})
.then(function(data) {
res.send(data)
})
`
答案 0 :(得分:0)
首先 - 您在同一日期获得多个条目确实很奇怪。我的猜测是,日期实际上在一天中的时间有所不同,即使它看起来并非如此。尝试提取日期部分。既然你还没有指定SQL方言我会在psql中给出例子,但是你应该能够在mysql等中找到类似的方法。
group: 'date_trunc('day', date)'
仅按日期部分分组(它将日期时间截断为日期,以便小时,分钟,秒等为0)
为了获得时间和bg_reading的数组,你需要选择它 - 在pg中该函数被称为array_agg
(mysql中的group_concat
)
attributes: [sequelize.fn('array_agg', sequelize.col('time')) ...],