mirror of
https://github.com/postgres/postgres.git
synced 2025-05-31 00:01:57 -04:00
Avoid mathematical inconsistency in example about avoiding division by
zero with a CASE expression. Per gripe from Russell Smith.
This commit is contained in:
parent
8984c8bbe2
commit
216e63bbce
@ -1,4 +1,4 @@
|
|||||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/syntax.sgml,v 1.120 2007/12/11 18:30:20 mha Exp $ -->
|
<!-- $PostgreSQL: pgsql/doc/src/sgml/syntax.sgml,v 1.121 2008/01/23 19:51:29 tgl Exp $ -->
|
||||||
|
|
||||||
<chapter id="sql-syntax">
|
<chapter id="sql-syntax">
|
||||||
<title>SQL Syntax</title>
|
<title>SQL Syntax</title>
|
||||||
@ -1740,15 +1740,15 @@ SELECT somefunc() OR true;
|
|||||||
used. For example, this is an untrustworthy way of trying to
|
used. For example, this is an untrustworthy way of trying to
|
||||||
avoid division by zero in a <literal>WHERE</> clause:
|
avoid division by zero in a <literal>WHERE</> clause:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
SELECT ... WHERE x <> 0 AND y/x > 1.5;
|
SELECT ... WHERE x > 0 AND y/x > 1.5;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
But this is safe:
|
But this is safe:
|
||||||
<programlisting>
|
<programlisting>
|
||||||
SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;
|
SELECT ... WHERE CASE WHEN x > 0 THEN y/x > 1.5 ELSE false END;
|
||||||
</programlisting>
|
</programlisting>
|
||||||
A <literal>CASE</> construct used in this fashion will defeat optimization
|
A <literal>CASE</> construct used in this fashion will defeat optimization
|
||||||
attempts, so it should only be done when necessary. (In this particular
|
attempts, so it should only be done when necessary. (In this particular
|
||||||
example, it would be best to sidestep the problem by writing
|
example, it would be better to sidestep the problem by writing
|
||||||
<literal>y > 1.5*x</> instead.)
|
<literal>y > 1.5*x</> instead.)
|
||||||
</para>
|
</para>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user