PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 28th, 2002, 10:01 AM
!zeBlSuDr !zeBlSuDr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 8 !zeBlSuDr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
auto calculate?

Newbie here.

I was wondering how to do something like this.

values entered for example into

goals = '2' , assists = '1'

and in the same table
I would like too have the DB automatically
calculate the

points = '3'

and further more how about percentages can pg convert entries say for

ie:

shots on goal = '10'

compared to total goals automatically %20.0

?


thanks alot!

Reply With Quote
  #2  
Old July 28th, 2002, 01:02 PM
ultraslacker's Avatar
ultraslacker ultraslacker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 56 ultraslacker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Yes, use TRIGGERs together with FUNCTIONs.


CREATE TRIGGER trigger_name
AFTER INSERT OR UPDATE ON table_name FOR EACH ROW
EXECUTE PROCEDURE function_name(goals, assists);


Check out the docs for more info.

Reply With Quote
  #3  
Old July 28th, 2002, 05:32 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Or better yet (and easier) just have your two fields in your base table, and define a view that handles the calculation:

Define table:
Code:
CREATE TABLE mytable(
player varchar(32) NOT NULL,         /* I'm guessing you'll want this */
goals int NOT NULL,
assists int NOT NULL
/* etc... any other columns you want... */
);


Define view:
Code:
CREATE VIEW vw_mytable AS
SELECT player, goals, assists, 
(goals + assists) AS points
FROM mytable
ORDER BY player ASC;


This means whenever you change value in 'goals' or 'assists', you don't have to re-calculate a field. Any amount of calculations you want can be dealt with this way. Calculated fields in general should be avoided from base tables, because the proper place to handle them is usually in views. This is one of the rules of database normalization.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #4  
Old July 28th, 2002, 09:38 PM
!zeBlSuDr !zeBlSuDr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 8 !zeBlSuDr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

great stuff! exactly what I needed.

I think I will really like postgres and the docs

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > auto calculate?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap