json - 输出为 JSON 时,我可以将数据库的列分组为数组吗?

标签 json database sequelize.js

我有多个相同的日期,我想对日期进行分组,所以我有一个 timebg_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)
  })
`

最佳答案

首先 - 您在同一日期获得多个条目确实很奇怪。我的猜测是日期实际上在一天中的时间不同,即使它看起来不是那样。尝试提取日期部分。由于您还没有指定 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')) ...],

关于json - 输出为 JSON 时,我可以将数据库的列分组为数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37934590/

相关文章:

php - SQL:获取结果,但添加从数字 2000 开始的列

mysql - Express - Sequelize - 在查询中添加外表字段

javascript - 返回多个 Sequelize 查询的结果

postgresql - Sequelize 跨两个表的唯一约束

json - Karate : How to set the Json value if object name contains period in an array

mysql - 多对多关系中的外键

json - 使用 Play! 将两个 json 对象渲染到 View 中斯卡拉2.3

mysql - 如何改进这个 SQL? (MySQL数据库)

javascript - 如何将 JSON 数组属性值从数组转换为键

php - 如何使用php从json中的两个mysql表中获取数据