X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 2BFCD9FB217 for ; Wed, 3 May 2006 07:09:20 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 05398-06-2 for ; Wed, 3 May 2006 07:09:03 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from ug-out-1314.google.com (uproxy.gmail.com [66.249.92.169]) by postgresql.org (Postfix) with ESMTP id 9D9D39FB251 for ; Wed, 3 May 2006 05:47:51 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id a2so27620ugf for ; Wed, 03 May 2006 01:47:49 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=GSuhfNYTPoVo52w2n8gGAHcFy4lLf5RfX23WIVAjtvYpj0uLzhDlxeIYHCcOcHdIbm4wOEh9sP0YsKXoltn3QTVKX8Cxloa5xMTDyvexRLEZZTEfm3vHXpZ28buK/kO5eMa+fT0EVHFVzL+ES4kTpQsBQhf/US3XM4w7/vOcJjQ= Received: by 10.78.45.13 with SMTP id s13mr32187hus; Wed, 03 May 2006 01:47:49 -0700 (PDT) Received: by 10.78.45.17 with HTTP; Wed, 3 May 2006 01:47:49 -0700 (PDT) Message-ID: <306f0afd0605030147i598ba049q9ef55a3f3b08847c@mail.gmail.com> Date: Wed, 3 May 2006 09:47:49 +0100 From: ed.temp.01@gmail.com To: pgsql-sql@postgresql.org Subject: selects on differing subsets of a query MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/34 X-Sequence-Number: 25065 First post, be gentle as I have terminology problems and so the subject might be wrongly worded. Say I have a table with fields ... gender diet_pref ... What I am trying to construct is a *single* query showing the total number of males in the table and also the total number of male vegetarians in the table, i.e. the 2nd value is computed on a subset of the records needed for the first value. As 2 queries this would be: select count(*) from mytab where gender=3D'm' select count(*) from mytab where gender=3D'm' and diet_pref=3D'veg' The table is big and I'd like to do the select where gender=3D'm' only once. (In the actual situation the select is on a date range) If there is a TFM, please point me at it with an indication of exactly what it is I am trying to achieve. If I'm trying to do something stupid, gentle advice would be appreciated.