Re: BUG #4057: SUM returns NULL when given no rows

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: BUG #4057: SUM returns NULL when given no rows
Дата
Msg-id 3159.1206490266@sss.pgh.pa.us
обсуждение исходный текст
Ответ на BUG #4057: SUM returns NULL when given no rows  ("Reginald Drake" <reggie.drake@gmail.com>)
Список pgsql-bugs
"Reginald Drake" <reggie.drake@gmail.com> writes:
> Doing something like "SELECT SUM(some_integer_column) FROM some_table WHERE
> FALSE" gives me NULL, where I would expect 0. Since COUNT does give a
> meaningful value when applied to zero columns, maybe SUM should do the same.

You might expect that, but the SQL spec is entirely clear on the matter:

            Case:

            a) If the <general set function> COUNT is specified, then the
              result is the cardinality of TXA.

            b) If AVG, MAX, MIN, or SUM is specified, then

              Case:

              i) If TXA is empty, then the result is the null value.
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

             ii) If AVG is specified, then the result is the average of the
                 values in TXA.

            iii) If MAX or MIN is specified, then the result is respec-
                 tively the maximum or minimum value in TXA. These results
                 are determined using the comparison rules specified in
                 Subclause 8.2, "<comparison predicate>".

             iv) If SUM is specified, then the result is the sum of the
                 values in TXA. If the sum is not within the range of the
                 data type of the result, then an exception condition is
                 raised: data exception-numeric value out of range.

            regards, tom lane

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

Предыдущее
От: "Reginald Drake"
Дата:
Сообщение: BUG #4057: SUM returns NULL when given no rows
Следующее
От: "David Rowley"
Дата:
Сообщение: Re: Possible problem with EXTRACT(EPOCH FROM TIMESTAMP)