Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id AAB3B9FB4E8 for ; Fri, 27 Jul 2007 16:31:37 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 20753-08 for ; Fri, 27 Jul 2007 16:31:35 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.241]) by postgresql.org (Postfix) with ESMTP id DBB969FB317 for ; Fri, 27 Jul 2007 16:31:34 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so178965ana for ; Fri, 27 Jul 2007 12:31:33 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Gey7usZtQPO8m2hqJfWKWM4WNq1K3+cgJibgCGQgIDn93Gx3c6qZkxHIX8SKQgMP5ZpHuwPHa6UJJjGqNHlP474i8IsdMa/TECHNjfpL1PiNxX4BrPZ744p2sv31sB/2Dna/z/ElVOpE90bGhyAnsRHW1ZunGEgPKh72jJepSdU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=FJI+6FEzqSA/GMvcc2tDPjGl8+FRSMpQA2GUb+W96CsGXllHRgyYgAbUE9cR5aotwJJ/Nn4ufpMOzsA40J8m+DUvSfIY91wYNBqjojbQ1243CQ2lXeysRb9xcH7EBZv3ZJl5IK2t5+sikTKtxBhxFAJTqGsLxgljst9AJZEnOZs= Received: by 10.100.127.1 with SMTP id z1mr3220965anc.1185564692471; Fri, 27 Jul 2007 12:31:32 -0700 (PDT) Received: by 10.100.33.20 with HTTP; Fri, 27 Jul 2007 12:31:32 -0700 (PDT) Message-ID: Date: Fri, 27 Jul 2007 14:31:32 -0500 From: "=?UTF-8?Q?Rodrigo_De_Le=C3=B3n?=" To: "Carol Cheung" Subject: Re: group by range of values Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <46AA41EC.8050101@consumercontact.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/174 X-Sequence-Number: 28909 On 7/27/07, I wrote: > On 7/27/07, Carol Cheung wrote: > > Something like: > > > > decade | average(salary) > > -------+----------------- > > 1940 | 69500 > > 1950 | 53333.33 > > 1960 | 53000 > > 1970 | 40333.33 > > CREATE TABLE tester ( > birth_year integer, > salary numeric(10,2) > ); > > SELECT > SUBSTRING(TO_CHAR(BIRTH_YEAR,'FM9999') FROM 1 FOR 3)||'0' > AS DECADE > , AVG(SALARY) AS AVG_SALARY > FROM TESTER > GROUP BY > SUBSTRING(TO_CHAR(BIRTH_YEAR,'FM9999') FROM 1 FOR 3)||'0' > ORDER BY DECADE; A bit simpler, if year is int: SELECT BIRTH_YEAR/10*10 AS DECADE , AVG(SALARY) AS AVG_SALARY FROM TESTER GROUP BY BIRTH_YEAR/10*10 ORDER BY DECADE;