Re: FW: Case Statement

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Tom Haddon <tom(at)betterhealthfoundation(dot)org>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: FW: Case Statement
Date: 2002-08-01 00:23:22
Message-ID: 20020731172012.W22782-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Christopher Kings-Lynne 2002-08-01 01:51:02 Re: negative queries puzzle
Previous Message Otto Hirr 2002-08-01 00:10:36 Re: Case Statement