Re: Merging lines with NULLs (with example data)

From: Harald Fuchs <hf0923x(at)protecting(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Merging lines with NULLs (with example data)
Date: 2005-10-25 13:42:24
Message-ID: 87u0f5sofz.fsf@srv.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

In article <djdp5l$1l4f$1(at)talisker(dot)lacave(dot)net>,
MaXX <bs139412(at)skynet(dot)be> writes:

> How can I "merge" this
> gday,count_udp,count_tcp
> '2005-10-20','','2'
> '2005-10-20','3',''
> '2005-10-21','','1'
> '2005-10-21','5',''

> into that:
> gday,count_udp,count_tcp
> '2005-10-20','3','2'
> '2005-10-21','5','1'

> in a single query???

Try something like that:

SELECT to_date (tstamp,'YYYY-MM-DD') AS gday,
sum(CASE WHEN proto = 'UDP' THEN 1 ELSE 0 END) AS count_udp,
sum(CASE WHEN proto = 'TCP' THEN 1 ELSE 0 END) AS count_tcp
FROM test
WHERE tstamp >= now() - INTERVAL '$days DAYS'
AND dst_port = $port
GROUP BY gday
ORDER BY gday

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Daryl Richter 2005-10-25 14:03:15 Re: Merging lines with NULLs (with example data)
Previous Message Tom Lane 2005-10-25 13:19:07 Re: convert timezone to string ...