Re: Group by with insensitive order

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Aram Fingal <fingal(at)multifactorial(dot)com>
Cc: Postgres-General General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Group by with insensitive order
Date: 2011-01-19 21:17:33
Message-ID: 20110119211733.GA21425@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jan 19, 2011 at 03:37:58PM -0500, Aram Fingal wrote:
> Suppose I'm doing a group by query like the following:
>
> SELECT drug1, drug2, AVG(response)
> FROM data
> GROUP BY drug1, drug2
>
> The problem is that the same drug may appear sometimes as drug1 and
> sometimes as drug2. So, for example, the combination "aspirin,
> acetaminophen" may also appear as "acetaminophen, aspirin" and I want
> these grouped together in the query. Is there a simple way to do
> this?

instead of drug1 write: least(drug1, drug2) and instead of drug2 write:
greatest(drug1, drug2) - both in select and group by.

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz / blog: http://www.depesz.com/
jid/gtalk: depesz(at)depesz(dot)com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

In response to

Browse pgsql-general by date

  From Date Subject
Next Message frank joerdens 2011-01-19 21:18:40 Re: debug_print_plan logs table alias used in join, not table name itself
Previous Message Jerry LeVan 2011-01-19 20:58:10 Re: How can I find the schema that a table belongs to?