2016-06-21 6 views
0

同じ日付の複数の値があるため、同じ日付の複数の日付があり、日付をグループ化してtimebg_readingという配列があります。これが私のJSONが吐き出すものです。JSONとして出力するときにデータベースの列を配列にグループ化できますか?

`[ 
    { 
    "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] 
    }... 
]` 

しかし、一体は非常に多くの繰り返し値が存在すると、なぜ複数の日付があるなぜ、今、私は見当がつかない:

`[ 
    { 
    "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 
    } 
]` 

私はこのようなこと何かを持って期待していました。これが私がSequeliseでそれを照会しようとした方法です。

` Log.findAll({ 
    where: { 
     bg_reading: {gt: 0} 
    }, 
    attributes: ['date', 'time', 'bg_reading'], 
    group: 'date', 
    }) 
    .then(function(data) { 
    res.send(data) 
    }) 
` 

答えて

0

まず、同じ日付の複数のエントリを取得するのは間違いです。私の推測では、たとえそれがそうではないにしても、日付は実際には時間が違うということでしょう。日付部分を抽出してみてください。あなたはSQLの方言を指定していないので、私はpsqlで例をあげるが、あなたは、MySQLで同様見つけることができるはずなど

group: 'date_trunc('day', date)' 
のみ日付の部分によって

ウィル・グループは、(それがダウン日時を切り捨て その日その時、分、秒などがあるので、0)

時間のための配列を得るために、あなたがそのように選択する必要がありbg_reading - PGにその関数が呼び出されたMySQLではarray_agggroup_concat

attributes: [sequelize.fn('array_agg', sequelize.col('time')) ...], 
関連する問題