Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 15262 invoked from network); 9 Dec 2009 17:56:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 9 Dec 2009 17:56:37 -0000 Received: (qmail 65847 invoked by uid 500); 9 Dec 2009 17:56:36 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 65752 invoked by uid 500); 9 Dec 2009 17:56:35 -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 65742 invoked by uid 99); 9 Dec 2009 17:56:35 -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 17:56:35 +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 17:56:27 +0000 Received: by ewy7 with SMTP id 7so8721398ewy.32 for ; Wed, 09 Dec 2009 09:56:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:from:date:message-id :subject:to:content-type; bh=FNvYr/TIT3DEZ1DgIA9fhwOvzPmnefFHUzJbV8dGS2g=; b=ff6Aa4FepPuu+XSF+BXudl0j5X1T0Vf/Yq7opr2ACUC0pg1zGhNdJbh0jE27lWnsO+ 420anMqbTJoDg3UOvtr7RZN6WNhPJNsmC8tSCqWB0y9AtUrorjKyZ9tjdrGlerG6EASg D4mPG10mLg3UEZwtODYSQIZFit8pOFRM9AL4I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=gNVYzD1CzTVul3IqOegORe+H1Nl5Lk67hhz9FPNiBG6ghEA8o4OeaqOj45NnBM4lqT 3rT8XcrxFgoNu/ddwkJxWM+O1RvLm43kDlE/6RDvfvsIiaV1p/lFdHhHyNFq2cwOrBZy C8LBrFEN+CCyC4ri9miyibvzR8INRgLsW0/DA= MIME-Version: 1.0 Received: by 10.216.87.75 with SMTP id x53mr3593510wee.13.1260381367119; Wed, 09 Dec 2009 09:56:07 -0800 (PST) From: Matteo Caprari Date: Wed, 9 Dec 2009 17:55:47 +0000 Message-ID: <1bca98390912090955w65c39cc9vddc1cb1cc750b5cc@mail.gmail.com> Subject: Help on grouping data by date To: user@couchdb.apache.org Content-Type: text/plain; charset=UTF-8 X-Virus-Checked: Checked by ClamAV on apache.org 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={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 the number of rows for each group.The client has to calculate the average on his own. _view/by_date?group_level=1 returns one row for each year, with the average amount for that year _view/by_date?group_level=2 returns one row for each mont, with the average amount for that month _view/by_date?group_level=3 returns one row for each day //map.js: function(doc) { emit(doc.date.split('-'), doc.amount); } // reduce.js function(keys, values, rereduce) { if (rereduce) { var result = {tot:0, count:0}; for (var idx in values) { result.tot += values[idx].tot; result.count += values[idx].count; } return result; } else { var result = {tot:sum(values), count:values.length}; return result; } } Thanks :) -- :Matteo Caprari matteo.caprari@gmail.com