Re: Unexpected behavior with CASE statement

Поиск
Список
Период
Сортировка
От Jimmy Choi
Тема Re: Unexpected behavior with CASE statement
Дата
Msg-id 5770602b0710031308j528247ecs24e0ddc7643aa458@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Unexpected behavior with CASE statement  ("Rodrigo De León" <rdeleonp@gmail.com>)
Ответы Re: Unexpected behavior with CASE statement
Список pgsql-general
This will work for this particular example. But what if my case
statement is more complicated than that? Example:

select
   metric_type,
   case metric_type
      when 0 then
         sum (1 / val)
      when 1 then
         sum (val)
      when 2 then
         max (val)
      when 3 then
         min (val)
   end as result
from metrics
group by metric_type

Thanks!

On 10/3/07, Rodrigo De León <rdeleonp@gmail.com> wrote:
> On 10/3/07, Jimmy Choi <yhjchoi@gmail.com> wrote:
> > I expect to get the following result set:
> >
> > metric_type | result
> > ------------+-------
> >     0       |   2
> >     1       |   3
>
> Try:
>
> SELECT   metric_type
>        , SUM(CASE metric_type
>                WHEN 0
>                  THEN 1 / val
>                WHEN 1
>                  THEN val
>              END) AS RESULT
>     FROM metrics
> GROUP BY metric_type
> ORDER BY metric_type
>

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

Предыдущее
От: "Rodrigo De León"
Дата:
Сообщение: Re: Unexpected behavior with CASE statement
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: Unexpected behavior with CASE statement