Reduce Calculations in SELECT

Поиск
Список
Период
Сортировка
От Carel Combrink
Тема Reduce Calculations in SELECT
Дата
Msg-id 20100831161646.38933og62kfyft1q@student.up.ac.za
обсуждение исходный текст
Ответы Re: Reduce Calculations in SELECT  (Josh Kupershmidt <schmiddy@gmail.com>)
Список pgsql-novice
Hi,

I have a SELECT statement that does the following (example only,
actual query more complex):
SELECT foo.id, mod(foo.one, foo.two)
   FROM my_table AS foo
   WHERE mod(foo.one, foo.two) > 2
       AND mod(foo.one, foo.two) < 6;

Mod is an expensive operation and it is calculated 3 times for the
same set of inputs. How can I get it to only calculate it once and and
use the result in the WHERE clause and return the value of the mod?

For this test my_table looks as follow:
=> SELECT * from my_table;
  id | one | two
----+-----+-----
   1 |   5 |   6
   2 |   1 |   5
   3 |   9 |   3
(3 rows)

Thank you
--
Carel Combrink
s25291930@tuks.co.za

This message and attachments are subject to a disclaimer. Please refer
to www.it.up.ac.za/documentation/governance/disclaimer/ for full
details. / Hierdie boodskap en aanhangsels is aan 'n vrywaringsklousule
onderhewig. Volledige besonderhede is by
www.it.up.ac.za/documentation/governance/disclaimer/ beskikbaar.


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

Предыдущее
От: Robert Dean
Дата:
Сообщение: Django + Postgressql
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Function Returning Table/Record