0

classification_indicator_id列にはいくつかの数字があります。この数字を1日系列で合計したいと思います。クエリの下に書きました

select
a.data_start::date,
a.segment1::integer as "Segment1"
from (
select
    data as data_start,
    (select sum(classification_indicator_id) from classifications where classification_indicator_id = 3)::integer as segment1
from
    generate_series('2013-03-25'::timestamp without time zone,
    '2013-04-01'::timestamp without time zone,
    '1 day'::interval) data
) a
group by
a.data_start,
a.segment1
ORDER BY data_start

しかし、私はいつも次のようなものを得ます:

date start|segment1
-------------------
2013-03-25|39
2013-03-26|39
2013-03-27|39
2013-03-28|39
2013-03-29|39
2013-03-30|39
2013-03-31|39
2013-04-01|39

私はそれが次のようなものであるべきだと確信しています:

date start|segment1
-------------------
2013-03-25|3
2013-03-26|4
2013-03-27|7
2013-03-28|9
2013-03-29|15
2013-03-30|22
2013-03-31|19
2013-04-01|5

SQL フィドル

4

2 に答える 2

1
select
    data.d::date,
    coalesce(sum(classification_indicator_id), 0)::integer as "Segment1"
from 
    classifications c
    right join
    generate_series(
        '2013-03-25'::timestamp without time zone,
        '2013-04-01'::timestamp without time zone,
        '1 day'::interval
    ) data(d) on data.d::date = c.data_start::date
where classification_indicator_id = 3
group by 1
ORDER BY 1
于 2013-04-04T09:46:21.997 に答える