pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Good <tomg@admin.nrnet.org>
To: Rich Shepard <rshepard@appl-ecosys.com>
Cc: PostgreSQL::SQL List <pgsql-sql@postgreSQL.org>
Subject: Re: [SQL] calculating percentages
Date: Mon, 12 Jul 1999 12:57:45 -0400 (EDT)
Message-ID: <Pine.LNX.3.96.990712124625.8886A-100000@admin.nrnet.org> (raw)
In-Reply-To: <Pine.LNX.4.10.9907120806280.25022-100000@salmo.appl-ecosys.com>

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 */ 




view thread (13+ messages)  latest in thread

Message-ID: <Pine.LNX.3.96.990712124625.8886A-100000@admin.nrnet.org>
Permalink:  ../Pine.LNX.3.96.990712124625.8886A-100000@admin.nrnet.org/
Also on:    postgresql.org/message-id/Pine.LNX.3.96.990712124625.8886A-100000@admin.nrnet.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tomg@admin.nrnet.org, rshepard@appl-ecosys.com
  Subject: Re: [SQL] calculating percentages
  In-Reply-To: <Pine.LNX.3.96.990712124625.8886A-100000@admin.nrnet.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