Re: [SQL] calculating percentages

From: Thomas Good <tomg(at)admin(dot)nrnet(dot)org>
To: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
Cc: "PostgreSQL::SQL List" <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] calculating percentages
Date: 1999-07-12 16:57:45
Message-ID: Pine.LNX.3.96.990712124625.8886A-100000@admin.nrnet.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, 12 Jul 1999, Rich Shepard wrote:

> > How does one do this: SELECT (current_value/target) * 100;
> > in postgres?
>
> Tom,
>
> In postgres or anything else, I'd select the records I wanted first, then
> assign each percentage to a variable; e.g.,
>
> pct_chng = (current_value/target) * 100;
>
> The varible, pct_chng, can be inserted in a view as another column and
> statistics can be calculated from these values.
>
> Rich

Rich,

I believe you hit the nail on the head...it's Monday don't you know
and the brain wasn't plugged in. I generally embed sql in perl and
can't imagine why it didn't occur to me to let perl's arithmetic
operators do the work on the values returned from pg.

I do alot of one-off statement handles ($sth->$fetchrow_array) where
the return value is an aggregate (so although the syntax is for a cursor
the stanza becomes a defacto singleton select). Then I let perl
do the rest, example (to suggest a startdate to a user):

$sth_timer = $dbh->prepare("
SELECT current_date - 30;
");
$sth_timer->execute or die;
$start_date = $sth_timer->fetchrow_array;
print("Start Date = $start_date? (Y/n): ");

Obviously, I'm an incurable shell scripter - but it does work!

Of course, I'll try the view you attached, and the function
another colleague sent but perl is probably what I'll use in the end.
Thanks for the wake up call!

Cheers,
Tom

------- North Richmond Community Mental Health Center -------

Thomas Good MIS Coordinator
Vital Signs: tomg@ { admin | q8 } .nrnet.org
Phone: 718-354-5528
Fax: 718-354-5056

/* Member: Computer Professionals For Social Responsibility */

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 1999-07-12 18:25:29 Left joins
Previous Message Tom Lane 1999-07-12 16:57:19 Re: [SQL] calculating percentages