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 9C1D1D9C82 for ; Tue, 18 Oct 2005 20:40:17 -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 68093-02 for ; Tue, 18 Oct 2005 23:40:13 +0000 (GMT) Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [151.155.5.143]) by svr1.postgresql.org (Postfix) with ESMTP id 1D03FD9C46 for ; Tue, 18 Oct 2005 20:40:15 -0300 (ADT) Received: from [172.16.1.100] grzm [61.197.227.146] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 1.6 $ on Linux via secured & encrypted transport (TLS); Tue, 18 Oct 2005 17:40:16 -0600 In-Reply-To: <1129207846.673463.305670@g44g2000cwa.googlegroups.com> References: <1129207846.673463.305670@g44g2000cwa.googlegroups.com> Mime-Version: 1.0 (Apple Message framework v734) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <4B8AECA9-3AA6-4E90-A84F-ECCB14334E76@myrealbox.com> Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: Query information needed Date: Wed, 19 Oct 2005 08:40:12 +0900 To: djzanky@gmail.com X-Mailer: Apple Mail (2.734) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.137 required=5 tests=[AWL=0.137] X-Spam-Level: X-Archive-Number: 200510/200 X-Sequence-Number: 23053 On Oct 13, 2005, at 21:50 , djzanky@gmail.com wrote: > Dear all, > > I have a table created with this specifications: > > CREATE TABLE cdr ( > calldate timestamp with time zone NOT NULL default now(), > clid varchar(80) NOT NULL default '', > src varchar(80) NOT NULL default '', > dst varchar(80) NOT NULL default '', > dcontext varchar(80) NOT NULL default '', > channel varchar(80) NOT NULL default '', > dstchannel varchar(80) NOT NULL default '', > lastapp varchar(80) NOT NULL default '', > lastdata varchar(80) NOT NULL default '', > duration bigint NOT NULL default '0', > billsec bigint NOT NULL default '0', > disposition varchar(45) NOT NULL default '', > amaflags bigint NOT NULL default '0', > accountcode varchar(20) NOT NULL default '', > uniqueid varchar(32) NOT NULL default '', > userfield varchar(255) NOT NULL default '' > ); > > I want to extract the number of calls placed in 1 hour and the average > call duration > > I'm working with this query: > > SELECT date_trunc('hour',calldate), duration FROM cdr WHERE src=601 > ORDER BY calldate; > > i tried several other queries but i'm not able to count the number of > calls in an hour (better in a time interval) and calculate the average > duration. For a first step, I'd build a view of create view cdr_by_hour_view as select date_trunc('hour,calldate) as callhour, duration from cdr; Then I'd select from this view using aggregates: select callhour, avg(duration) as avg_duration from cdr where src = 601 group by callhour; You could write it This should help you with the simple case of just hourly averages. For averages on any given interval I think it's a bit more involved (as general cases generally are). Hope this helps! Michael Glaesemann grzm myrealbox com