Skip site navigation (1) Skip section navigation (2)

function to operate on same fields, different records?

From: will trillich <will(at)serensoft(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: function to operate on same fields, different records?
Date: 2001-03-29 19:17:29
Message-ID: 20010329131729.B27033@mail.serensoft.com (view raw or flat)
Thread:
Lists: pgsql-general
is this kind of thing possible---?

	select gpa(student) from student where id=7121;
	select gpa(course) from course where id=29931;
	select gpa(prof) from prof where id=1321;

i've got several tables each of which have

	create table <various-and-sundry> (
		...
		a int4,
		b int4,
		c int4,
		d int4,
		f int4,
		...
	);

since i'd like to AVOID this nonsense--

	create view courseGPA as
	select *,
			(a * 4 + b * 3 + c * 2 + d)
			/
			(a + b + c + d + f) as gpa
		from course;

	create view profGPA as
	select *,
			(a * 4 + b * 3 + c * 2 + d)
			/
			(a + b + c + d + f) as gpa
		from prof;

	create view studentGPA as
	select *,
			(a * 4 + b * 3 + c * 2 + d)
			/
			(a + b + c + d + f) as gpa
		from student;

i'd rather be able to do this--

	create function gpa( unknowntableTuple ) returns float8 as '
		select
			($1.a * 4 + $1.b * 3 + $1.c * 2 + $1.d)
			/
			($1.a + $1.b + $1.c + $1.d + $1.f)
	' language 'sql';

any chance of working something like that? if so, how? if not,
well, waaah!

-- 
does a brain cell think?

will(at)serensoft(dot)com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

Responses

pgsql-general by date

Next:From: Peter EisentrautDate: 2001-03-29 19:39:21
Subject: Re: missing data
Previous:From: will trillichDate: 2001-03-29 19:09:50
Subject: Re: timestamp/function question

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group