X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5DB27DACCC for ; Tue, 25 Oct 2005 11:01:27 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 36421-06 for ; Tue, 25 Oct 2005 14:01:19 +0000 (GMT) Received: from website.brandywine.com (mailcheck.brandywine.com [146.145.113.9]) by svr1.postgresql.org (Postfix) with ESMTP id BE343DA0DE for ; Tue, 25 Oct 2005 11:01:21 -0300 (ADT) X-eSCM-MailFrom: Received: from BAM_EXCHANGE (10.1.0.100) by website.brandywine.com (10.1.0.9); Tue, 25 October 2005 10:04:57 -0400 Received: from [10.1.0.60] (gen-wayne [10.1.0.60]) by bam_exchange.brandywine.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13) id R6ZYYMWJ; Tue, 25 Oct 2005 10:00:57 -0400 Message-ID: <435E3B23.1070909@brandywine.com> Date: Tue, 25 Oct 2005 10:03:15 -0400 From: Daryl Richter Reply-To: daryl@brandywine.com Organization: Brandywine Asset Management User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 Cc: pgsql-sql@postgresql.org Subject: Re: Merging lines with NULLs (with example data) References: <87u0f5sofz.fsf@srv.protecting.net> In-Reply-To: <87u0f5sofz.fsf@srv.protecting.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.054 required=5 tests=[AWL=0.054] X-Spam-Level: X-Archive-Number: 200510/286 X-Sequence-Number: 23139 Harald Fuchs wrote: > In article , > MaXX 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 > Or, via a subquery: select distinct to_date(tstamp,'YYYY-MM-DD') as gday, ( select count(id) from test t1 where proto='UDP' and to_date(t1.tstamp,'YYYY-MM-DD') = to_date(test.tstamp,'YYYY-MM-DD') ) as count_udp, ( select count(id) from test t1 where proto='TCP' and to_date(t1.tstamp,'YYYY-MM-DD') = to_date(test.tstamp,'YYYY-MM-DD') ) as count_tcp from test where tstamp >= (now() - interval '6 days' ) and dst_port = 2290 order by gday; Harald's solution is better for your particular case and will almost certainly be faster, but subqueries are good to know how to do. :) > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org -- Daryl Richter Platform Author & Director of Technology (( Brandywine Asset Management ) ( "Expanding the Science of Global Investing" ) ( http://www.brandywine.com ))