我有一个包含net_amount
字段和last_updated_time
字段的事务表,其中last_updated_time
以毫秒为单位存储。我需要使用月,年或日期来获得总金额组。我怎么能在PostgreSQL中做到这一点?
我的表格如下所示:
+------------+-------------------+
| net_amount | last_updated_time |
+------------+-------------------+
| 100 | 1470286872831 |
+------------+-------------------+
| 200 | 1471594713801 |
+------------+-------------------+
| 300 | 1471594651335 |
+------------+-------------------+
并期待结果为:
+----------+---------------+
| month | sum_of_amount |
+----------+---------------+
| january | 1000 |
+----------+---------------+
| february | 2000 |
+----------+---------------+
| --- | ---- |
+----------+---------------+
答案 0 :(得分:3)
您可以执行以下操作:
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));
我刚刚在我的项目数据库中查看它,它对我有用。
编辑:我将last_update_time转换为时间戳,如@a_horse_with_no_name所指出。
答案 1 :(得分:1)
如果我正确理解您的问题,您可以尝试执行以下操作(Java 8)
long day1MSec, day2MSec ;
LocalDate localDate1 = LocalDate.of( 2011 , Month.JULY , 3 );
LocalDate localDate2 = LocalDate.of( 2011 , Month.JULY , 25 );
final long msPerDay = 24 * 60 * 60 * 1000;//milisec per day
day1MSec = localDate1.toEpochDay() * msPerDay;
day2MSec = localDate2.toEpochDay() * msPerDay;
//now your sql would look something like
String sql = "select sum(amount)from transaction group by last_updated having last_updated between "+day1MSec + " and "+day2MSec;
因此,您需要在Java代码中执行的操作是将日期转换为毫秒。如果您想使用数月或数年,只需调整您的日期以匹配一个月或一年的日期。
LocalDate localDate1 = LocalDate.of( 2011 , Month.JANUARY , 1 );
更新:对于低于8的java版本,您可以使用
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY).parse("2015-06-25");
long mSec = date.getTime(); //this returns miliseconds
奇怪的是,两个版本的结果不同,所以我想知道第一个版本中是否存在错误(因为第二个版本似乎给出了正确的结果)
答案 2 :(得分:0)
您可以编写一个查询,为您提供所有准备好的聚合:
create table Transaction
(
amount DECIMAL(9,2),
last_updated_time BIGINT
);
insert into Transaction values (10, 1472680800000); -- 2016-09-01
insert into Transaction values (20, 1473458400000); -- 2016-09-10
insert into Transaction values (30, 1474408800000); -- 2016-09-21
insert into Transaction values (5, 1475272800000); -- 2016-10-01
insert into Transaction values (2, 1475272800000); -- 2016-10-01
insert into Transaction values (7, 1475272800000); -- 2016-10-02
insert into Transaction values (15, 1478818800000); -- 2016-11-11
在与to_timestamp
的合并中使用EXTRACT来提取您想要的内容(MONTH
,YEAR
),然后group by
。
select
sum(amount),
EXTRACT(MONTH FROM to_timestamp(last_updated_time/1000)) as month
from Transaction
group by month order by month asc;
sum | month
-------+-------
60.00 | 9
14.00 | 10
15.00 | 11
select
sum(amount),
EXTRACT(YEAR FROM to_timestamp(last_updated_time/1000)) as year
from Transaction
group by year order by year asc;
sum | year
-------+--------
89.00 | 2016
最后,为了解决我的评论,您EXTRACT
中的年份和月份select
可以group by
,然后cmd 2>&1 | pbcopy
他们(see here)。