I have the following simplified postgresql table 'fees':
Table "public.fees"
id | datetime | fee
---+-----------------------------+----
1 | 2013-10-17 09:11:00.138021 | 5
2 | 2013-10-17 09:15:02.848841 | 20
4 | 2013-10-18 09:17:40.784396 | 40
5 | 2013-10-18 09:29:21.789913 | 10
16 | 2013-10-18 09:39:38.308201 | 10
17 | 2013-10-19 09:40:09.507662 | 10
18 | 2013-10-19 09:40:14.310772 | 10
22 | 2013-10-20 09:54:37.183343 | 40
35 | 2013-10-21 10:32:28.619779 | 10
39 | 2013-10-21 10:34:50.830838 | 10
I want a sql result like this:
| datetime | feeSum
---------------+----
| 2013-10-17 | 25
| 2013-10-18 | 60
| 2013-10-19 | 20
| 2013-10-20 | 40
| 2013-10-21 | 20
which sums each day's fee to feeSum.
I've searched a lot and try this:
create view temp as
select datetime, fee from fees
where
datetime::date>='2013-10-17' and
datetime::date<='2013-10-21';
do $$
declare
fromdate date := '2013-10-17';
todate date := '2013-10-21';
begin
for d in fromdate..todate
loop
select sum(fee) as feeSum, d as datetime from temp
where
datetime::date=d;
end loop;
end;
$$;
but got error:
ERROR: invalid input syntax for integer: "2013-10-17"
and I can't find how to define date variable. Or is there another way to get my result?
date_trunc('day', datetime) as date.where datetime::date>='2013-10-17' and datetime::date<='2013-10-21';no error.fromdate date := '2013-10-17';here cause error