pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: dnrickner@taylor.edu
To: pgsql-bugs@postgresql.org
Subject: BUG #8471: subquery where not being applied until outer query
Date: Thu, 26 Sep 2013 15:22:40 +0000
Message-ID: <E1VPDOq-0002Sx-5f@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      8471
Logged by:          Dan Rickner
Email address:      dnrickner@taylor.edu
PostgreSQL version: 9.2.4
Operating system:   CentOS
Description:        

Our ERP stores student GPA values as a text string.  I am trying to select
only valid gpa values (a number between 0 and 4.0).  I have a function
called numeric that returns a bool if the value can be converted to a
number.  When I run my query I get errors about values that should not be
considered in my outer where statement.  The inner where is supposed to
filter out the bad data values:


-- function to return if a character string can be converted to a number
create or replace function isnumeric(varchar) returns boolean as $$
declare x numeric;
begin
	x = $1::numeric;
	return true;
exception when others then
	return false;
end
$$
language plpgsql
immutable;


-- test table
create table tbl
(
	id serial not null,
	gpa varchar(6) null
);


-- insert bad data
insert into tbl (gpa) values ('A'), ('2.0'), ('12.5'), ('3.45'), (''),
('-'), ('-2.3'), ('-5');




select *
from
( -- this subquery returrns only numeric values
	select a.id, trunc(a.gpa::numeric, 2) as gpa
	from
	(
		select id, gpa, isnumeric(gpa) as num
		from tbl
	) as a
	where a.num = true
) as b
-- filter the numeric values to the 4.0 range
where b.gpa between 0.0 and 4.0










-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



Message-ID: <E1VPDOq-0002Sx-5f@wrigleys.postgresql.org>
Permalink:  ../E1VPDOq-0002Sx-5f@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/E1VPDOq-0002Sx-5f@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: dnrickner@taylor.edu
  Subject: Re: BUG #8471: subquery where not being applied until outer query
  In-Reply-To: <E1VPDOq-0002Sx-5f@wrigleys.postgresql.org>
  References: <E1VPDOq-0002Sx-5f@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox