Shi*_* EC 6 java sql postgresql
我有一个包含net_amount字段和last_updated_time字段的事务表,其中last_updated_time存储为毫秒.我需要使用月,年或日期来获得总金额组.我怎么能在PostgreSQL中做到这一点?
我的表如下所示:
+------------+-------------------+
| net_amount | last_updated_time |
+------------+-------------------+
| 100 | 1470286872831 |
+------------+-------------------+
| 200 | 1471594713801 |
+------------+-------------------+
| 300 | 1471594651335 |
+------------+-------------------+
Run Code Online (Sandbox Code Playgroud)
期待结果如下:
+----------+---------------+
| month | sum_of_amount |
+----------+---------------+
| january | 1000 |
+----------+---------------+
| february | 2000 |
+----------+---------------+
| --- | ---- |
+----------+---------------+
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
SELECT sum(amount), date_trunc('month', to_timestamp(last_updated_time/1000))
FROM transaction
GROUP BY date_trunc('month', to_timestamp(last_updated_time/1000));
Run Code Online (Sandbox Code Playgroud)
我刚刚在我的副项目数据库中检查了它,它对我有用。
编辑:我将last_update_time转换为时间戳,如@a_horse_with_no_name所指出的。
| 归档时间: |
|
| 查看次数: |
1012 次 |
| 最近记录: |