Re: FW: Case Statement

Поиск
Список
Период
Сортировка
От Stephan Szabo
Тема Re: FW: Case Statement
Дата
Msg-id 20020731172012.W22782-100000@megazone23.bigpanda.com
обсуждение исходный текст
Ответ на FW: Case Statement  ("Tom Haddon" <tom@betterhealthfoundation.org>)
Список 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.





В списке pgsql-sql по дате отправления:

Предыдущее
От: "Otto Hirr"
Дата:
Сообщение: Re: Case Statement
Следующее
От: "Christopher Kings-Lynne"
Дата:
Сообщение: Re: negative queries puzzle