From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Tom Haddon <tom@betterhealthfoundation.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: FW: Case Statement
Date: Wed, 31 Jul 2002 17:23:22 -0700 (PDT)
Message-ID: <20020731172012.W22782-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <NEBBIHDGCLBEJMPFAMLAOEKDCHAA.tom@betterhealthfoundation.org>
On Wed, 31 Jul 2002, Tom Haddon wrote:
> Hi Stephan,
>
> I have a lot of fields, so I'm not sure if a function or case is the way to
> go. Basically, I have, say 50 boolean fields that are being evaluated, and I
> want to have a column which is the sum of the number of "TRUE" values of
> those 50 columns, and then ORDER BY that column. So, for example of the 50
> fields to be evaluated, the SELECT statement should return records ordered
> by those that match the most number of criteria. Does that make sense? Your
> example below may actually work for me, on the other hand:
>
> "case when field1 is true then 1 else 0 end +
> case when field2 is true then 2 else 0 end +
> case when field3 is true then 4 else 0 end
>
> Let's suppose field1 is true and field2, field3 are not, would it return 1,
> and if all three are true it would return 7 for the case? If so, I think
> that's the way to go, as all this is being dynamically built in any case.
Yes, that's what it should do (I did the 2 variable case and all 4
combinations to try getting all 0-3). If you're doing many more selects
than update/inserts to the table where you want to do this (and the fields
you care about is a constant set) then a trigger on insert/update is the
way to go and just store the value.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sszabo@megazone23.bigpanda.com, tom@betterhealthfoundation.org
Subject: Re: FW: Case Statement
In-Reply-To: <20020731172012.W22782-100000@megazone23.bigpanda.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox