Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TSGJj-0005G8-BG for pgsql-sql@postgresql.org; Sun, 28 Oct 2012 00:01:27 +0000 Received: from na3sys009aog129.obsmtp.com ([74.125.149.142]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TSGJb-0002pm-A7 for pgsql-sql@postgresql.org; Sun, 28 Oct 2012 00:01:26 +0000 Received: from mail-qa0-f46.google.com ([209.85.216.46]) (using TLSv1) by na3sys009aob129.postini.com ([74.125.148.12]) with SMTP ID DSNKUIx1zH4uz4b3IymGiNKfgipIZ8KtyraG@postini.com; Sat, 27 Oct 2012 17:01:19 PDT Received: by mail-qa0-f46.google.com with SMTP id c26so899068qad.19 for ; Sat, 27 Oct 2012 17:01:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:x-gm-message-state; bh=n8wVMjRIscVF5mpSQ77hLHDBYY2j/LXymiKBSfidaLU=; b=XwqEdnV1JFqbfF0Ly/FrG3p0c1OP71Xk1c0Ff7N5oJd05UG/Eeu0S+lGrEayx0GkSF /X8625PoV+HFdBK8W061uKakgs1ZpCYMkVrnQq6kMKV456o0tfw6oWzC/RMEtbetNuuj 2KqjNadl6KoJUu6fDJ94bQpJwYNcoZ5C3ush1bdji5EN1tGjGzBjyp0k1XbbnKaFj2L/ Afe7RAVMAIsNWIceWgXLbBU82wDFTpGtpDW8MCA7LQszMzZAe8+Fhe2dkVkue79Mua1e WwTH5fUEvBP6NlT+5A1/ahj+Rsv4ZdxDEu2vm6gOcjNlgcSpAYYywQv8nMvqEX2jB1eJ JDNw== Received: by 10.49.105.229 with SMTP id gp5mr18116647qeb.35.1351382475720; Sat, 27 Oct 2012 17:01:15 -0700 (PDT) Received: from [198.206.42.50] (rrcs-24-172-201-241.central.biz.rr.com. [24.172.201.241]) by mx.google.com with ESMTPS id hb7sm3461648qab.20.2012.10.27.17.01.14 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 27 Oct 2012 17:01:15 -0700 (PDT) Message-ID: <508C75D1.2070906@noaa.gov> Date: Sat, 27 Oct 2012 20:01:21 -0400 From: Mark Fenbers User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: complex query Content-Type: multipart/mixed; boundary="------------030503080902050804050401" X-Gm-Message-State: ALoCoQnklJgw4t59Ue7B7+T/M9PcxYaF3EIWQ2H/QUHRrHYsSUr/KPC7e75iJ95oayjQZlGmqBc7 X-Pg-Spam-Score: -4.2 (----) X-Archive-Number: 201210/49 X-Sequence-Number: 36920 This is a multi-part message in MIME format. --------------030503080902050804050401 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit I have a query:
SELECT id, SUM(col1), SUM(col2) FROM mytable WHERE condition1 = true GROUP BY id;

This gives me 3 columns, but what I want is 5 columns where the next two columns -- SUM(col3), SUM(col4) -- have a slightly different WHERE clause, i.e., WHERE condition2 = true.

I know that I can do this in the following way:
SELECT id, SUM(col1), SUM(col2), (SELECT SUM(col3) FROM mytable WHERE condition2 = true), (SELECT SUM(col4) FROM mytable WHERE condition2 = true) FROM mytable WHERE condition1 = true GROUP BY id;

Now this doesn't seem to bad, but the truth is that condition1 and condition2 are both rather lengthy and complicated and my table is rather large, and since embedded SELECTs can only return 1 column, I have to repeat the exact query in the next SELECT (except for using "col4" instead of "col3").  I could use UNION to simplify, except that UNION will return 2 rows, and the code that receives my resultset is only expecting 1 row.

Is there a better way to go about this?

Thanks for any help you provide.
Mark

--------------030503080902050804050401 Content-Type: text/x-vcard; charset=utf-8; name="mark_fenbers.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="mark_fenbers.vcf" begin:vcard fn:Mark Fenbers n:Fenbers;Mark org:Ohio River Forecast Center;Hydrometeorological Analysis & Support Unit adr:1901 South OH-134;;National Weather Service;Wilmington;OH;45177-9708;USA email;internet:Mark.Fenbers@noaa.gov title:Senior Meteorologist tel;work:937-383-0430 tel;fax:937-383-0033 url:weather.gov/ohrfc version:2.1 end:vcard --------------030503080902050804050401--