From user-return-7939-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Wed Dec 09 18:53:51 2009 Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 55571 invoked from network); 9 Dec 2009 18:53:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 9 Dec 2009 18:53:51 -0000 Received: (qmail 90331 invoked by uid 500); 9 Dec 2009 18:53:49 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 90241 invoked by uid 500); 9 Dec 2009 18:53:49 -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 90231 invoked by uid 99); 9 Dec 2009 18:53:49 -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 18:53:49 +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 paul.joseph.davis@gmail.com designates 209.85.223.190 as permitted sender) Received: from [209.85.223.190] (HELO mail-iw0-f190.google.com) (209.85.223.190) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Dec 2009 18:53:41 +0000 Received: by iwn28 with SMTP id 28so5452997iwn.13 for ; Wed, 09 Dec 2009 10:53:20 -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=4yLKvakfbsqKzHhc2uICzQdcG3TV7oeV0QlPRHE0Avs=; b=VO2KhSATqEK6x0rkSztASApUlcG8mVuvxiPE2Jjk7tac0EMuFRVrCJX4UYalszOLdv jeSRAxPfvyk9W3UknMC2uu0mQHB3QpvcSdo/xwM1dbPHeHeB4KsF1yd31G2TyGT1FePn 6W+Wn3VeucJL5tPJAKQuA3MTtUt81eZZvQofA= 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=I2uiAb1PxJWD9KhFbh7IDh25Cg8UFJP12cXDYZf9dJ/6jxaFGRZa+n2OKHvmfQVzpE MnJjniWTb6KXbNvKzsXtVKR4z6G+gMc6/uMrdRSmEdBD51Z2puGQ+Lt/awKC1uIKUI0P izP0CuyLtcwoFx4IVvPwcDzC2WSwRsXeu47bk= MIME-Version: 1.0 Received: by 10.231.156.205 with SMTP id y13mr146590ibw.27.1260384800157; Wed, 09 Dec 2009 10:53:20 -0800 (PST) In-Reply-To: <1bca98390912090955w65c39cc9vddc1cb1cc750b5cc@mail.gmail.com> References: <1bca98390912090955w65c39cc9vddc1cb1cc750b5cc@mail.gmail.com> From: Paul Davis Date: Wed, 9 Dec 2009 13:53:00 -0500 Message-ID: Subject: Re: Help on grouping data by date To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org 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 =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) { > =A0 =A0 =A0 =A0emit(doc.date.split('-'), doc.amount); > } > > // reduce.js > function(keys, values, rereduce) { > =A0 =A0 =A0 =A0if (rereduce) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0var result =3D {tot:0, count:0}; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0for (var idx in values) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0result.tot +=3D values[idx= ].tot; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0result.count +=3D values[i= dx].count; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return result; > =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0else { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0var result =3D {tot:sum(values), count:val= ues.length}; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return result; > =A0 =A0 =A0 =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