MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 20th, 2005, 07:50 AM
Sanjin Sanjin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 2 Sanjin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 58 sec
Reputation Power: 0
Updating Table

I have two tables with following content:

TABLE_A
|-ID-|-GROUPID------|-MARK--|
|-1--|-------1-------|--65.54-|
|-2--|-------1-------|--75.60-|
|-3--|-------1-------|---58---|
|-4--|-------2-------|--55.78-|
|-5--|-------3-------|--88.00-|
|-6--|-------3-------|--56.70-|
|-7--|-------4-------|--66.47-| and so on, many enteries

TABLE_B
|--GROUPID------|--AVRG_MARK------|
|-------1--------|-----0.000---------|
|-------2--------|-----0.000---------|
|-------3--------|-----0.000---------|
|-------4--------|-----0.000---------| and so on ........

I want to update TABLE_B's AVRG_MARK column where TABLE_B.GROUPID = TABLE_A.GROUPID so that TABLE_B's AVRG_MARK contains averages.

For example, 1st row in TABLE_B will get updated and contain:

|-1-|---66.88---| where 66.88 = (65.54+75.60+58)/3

Those 3 numbers in brackets come from TABLE_A's MARK column and I devide by 3 since there are 3 enteries

I want to do this for the whole TABLE_B. Can you help me out guys, I'm a bit stuck.

I've built my database tables using MySQL.

Reply With Quote
  #2  
Old September 20th, 2005, 08:18 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I'm curious, why are you storing the average mark in a database?
It can be calculated quickly from a query.

I think the following should achieve TABLE_B:
SELECT GROUPID, AVG(mark) FROM TABLE_A GROUP BY GROUPID

Keep in mind, my query doesn't help you store it in the database.
I would use this everywhere you would have been using SELECT * FROM TABLE_B

Reply With Quote
  #3  
Old September 20th, 2005, 08:41 AM
Sanjin Sanjin is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 2 Sanjin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by MadCowDzz
I'm curious, why are you storing the average mark in a database?
It can be calculated quickly from a query.

I think the following should achieve TABLE_B:
SELECT GROUPID, AVG(mark) FROM TABLE_A GROUP BY GROUPID

Keep in mind, my query doesn't help you store it in the database.
I would use this everywhere you would have been using SELECT * FROM TABLE_B


My TABLE_B actually contains 2 more columns (Pref_1, Pref_2) that I need to use in order to perform a selection
algorithm based on a highest average mark.
I've posted this question on another forum and this is the answer that I've received:

update table_b
set avrg_mark = x.av
from table_b join
(select group_id, ave(mark) av
from table_a
group by group_id) x on table_b group_id = x.group_id
group by group_id)

However, I have no clue what this does, do you ?

Reply With Quote
  #4  
Old September 20th, 2005, 10:22 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
It's using a subquery to create a 'table' called x containing the average you need. That table is being joined to table_b, so you can then use that average to update your average field in table_b.

If possible, though (and I don't know how your pref_* fields interact with your average, but I'll take your word for it if you say it's not), you shouldn't store the results of calculations like that in your db. Like MadCowDzz said, you can calculate averages on the fly pretty easily. If you store them statically, though, you have to constantly re-run and update your rows to keep it current.
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Updating Table


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway