質問

columns of table

date                |count|ip
2014-05-14 11:21:00 |2    | 222.222.222.22
2014-05-14 11:22:00 |4    | 232.232.232.23
2014-05-14 11:24:00 |1    | 222.222.222.22

this is working fine for me but this solution give me value null for series but I need value 0 instead of null

In the current query I can't get exact sequence without where I get fill missing sequence

Current Query:

SELECT sum(date),minute
FROM generate_series('2014-05-14 0:00:00'::timestamp, '2014-05-14 23:59:59', 
              '1 minute') AS minutes(minute)
    LEFT JOIN table ON minute=date_trunc('minute',table.date) 
where switch_ip='232.232.232.23'
GROUP BY minute

Current Output :

2014-05-14 11:22:00 |4

Required Output :

1) If in where ip = 232.232.232.23 and date >= '2014-05-14 00:00:00' and date <= '2014-05-14 23:59:59'

2014-05-14 11:21:00 | 0
2014-05-14 11:22:00 | 4
2014-05-14 11:23:00 | 0

2) If in where ip 222.222.222.22 and date >= '2014-05-14 00:00:00' and date <= '2014-05-14 23:59:59'

2014-05-14 11:21:00 | 2
2014-05-14 11:22:00 | 0
2014-05-14 11:23:00 | 0    
2014-05-14 11:24:00 | 1

3)If in where date >= '2014-05-14 00:00:00' and date <= '2014-05-14 23:59:59'

2014-05-14 11:21:00 | 2
2014-05-14 11:22:00 | 4
2014-05-14 11:23:00 | 0    
2014-05-14 11:24:00 | 1
役に立ちましたか?

解決

By placing the ip condition in the where clause you turned the outer join into an inner join. Move it to the join condition

select coalesce(sum("count"), 0), minute
from
    generate_series(
        '2014-05-14 0:00:00'::timestamp,
        '2014-05-14 23:59:59', 
        '1 minute'
    ) minutes(minute)
    left join
    t on
        minutes.minute = date_trunc('minute', t.date)
        and
        ip = '232.232.232.23'
group by minute
order by minute

http://sqlfiddle.com/#!15/31483/5

To have the timestamp as a comma separated string replace

select coalesce(sum("count"), 0), minute

by

select 
    coalesce(sum("count"), 0), 
    to_char(minute, 'YYYY,MM,DD,HH24,MI') as minute
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top