Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 78136 invoked from network); 9 Dec 2009 19:44:33 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 9 Dec 2009 19:44:33 -0000 Received: (qmail 98586 invoked by uid 500); 9 Dec 2009 19:44:32 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 98545 invoked by uid 500); 9 Dec 2009 19:44:32 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 98535 invoked by uid 99); 9 Dec 2009 19:44:31 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Dec 2009 19:44:31 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of matteo.caprari@gmail.com designates 209.85.219.215 as permitted sender) Received: from [209.85.219.215] (HELO mail-ew0-f215.google.com) (209.85.219.215) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Dec 2009 19:44:23 +0000 Received: by ewy7 with SMTP id 7so8845036ewy.32 for ; Wed, 09 Dec 2009 11:44:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=HWBOsbd6zftWQuBWlq03RLXECo91H8kd61aQMPmt0yY=; b=YpCy9CVS55RRlJ3fILMIaaS3az0ecuWTx/03NhjNHfMBs7cpgwq29QV1TVYCn44UfE 7TivcGJasSqpzxWGWneOXRzLQwdxvEFnFwjIQTk1TtplUTWVxb0+IYfXoZ+/ZgrLz8gH 7sJdM4LRxHWqneYwKL+6mvOc807h9EDMckmSo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=BQO+4w7BYUvoyOIbDkbhucwXBLHyoZ8/ycYfgY4fnsityw+gAoJyMhrh6ry+FE01Vv XVSROiBwZ1ZAqX99xxMUM677OiDdLcjkCt03Nd/lEMpJzFhvS9htwk+VE5V9kqCpBn1S ijUARl7drGUBiOEkO4kHI2w5Wxpzn47Km4CRk= MIME-Version: 1.0 Received: by 10.216.85.9 with SMTP id t9mr127408wee.79.1260387843220; Wed, 09 Dec 2009 11:44:03 -0800 (PST) In-Reply-To: References: <1bca98390912090955w65c39cc9vddc1cb1cc750b5cc@mail.gmail.com> From: Matteo Caprari Date: Wed, 9 Dec 2009 19:43:43 +0000 Message-ID: <1bca98390912091143n7fa05f0ged53dd9ef2d913c1@mail.gmail.com> Subject: Re: Help on grouping data by date To: user@couchdb.apache.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org Hi Paul. This is interesting. I assumed that the rereduce may be feeded with output from the rereduce itself, while you seem to imply that the output of the rereduce is always a final value (ie is one row in the view output). Am I misreading your mail? Thanks for the pointers. On Wed, Dec 9, 2009 at 6:53 PM, Paul Davis wr= ote: > On Wed, Dec 9, 2009 at 12:55 PM, Matteo Caprari > wrote: >> Hello. >> >> I'm learning couchdb and I'd appreciate if someone could have look and >> comment on the code and description that follows. >> >> Given a set of documents like {date:"2009-10-11", amount:10500}, I'm >> using the view below >> and group_level=3D{1,2,3} to group by year, month or day and return the >> average amount for each group. >> >> I don't think it's possible to write a rereduce-friendly averaging >> function; Instead, I return an object >> that holds the total amount and =C2=A0the number of rows for each group.= The >> client has to calculate the average on his own. >> >> _view/by_date?group_level=3D1 returns one row for each year, with the >> average amount for that year >> _view/by_date?group_level=3D2 returns one row for each mont, with the >> average amount for that month >> _view/by_date?group_level=3D3 returns one row for each day >> >> //map.js: >> function(doc) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0emit(doc.date.split('-'), doc.amount); >> } >> >> // reduce.js >> function(keys, values, rereduce) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0if (rereduce) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0var result =3D {t= ot:0, count:0}; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for (var idx in v= alues) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0result.tot +=3D values[idx].tot; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0result.count +=3D values[idx].count; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0} >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return result; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0} >> =C2=A0 =C2=A0 =C2=A0 =C2=A0else { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0var result =3D {t= ot:sum(values), count:values.length}; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return result; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0} >> } >> >> Thanks :) >> >> -- >> :Matteo Caprari >> matteo.caprari@gmail.com >> > > Matteo, > > That all looks pretty spot on. Though there's nothing keeping you from > adding a line in your rereduce code to just do result.average =3D > result.tot / result.count before returning. You can see a similar > example in the reduce.js Futon tests of a similar calculation for > standard deviation. > > There's also the view snippets page on the wiki [1]. > > Paul Davis > > [1] http://wiki.apache.org/couchdb/View_Snippets > --=20 :Matteo Caprari matteo.caprari@gmail.com