Jump to content


Photo

A SELECT using SUM and AVG that returns the wrong result


  • Please log in to reply
7 replies to this topic

#1 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 29 September 2006 - 11:38 PM

I get 2 different results from a query that I thought would return the same result. The reason I want to use the first example (which returns an incorrect value for SUM) is that I also need the AVG value and don't want to make 2 queries if at all possible.

If I set up my query this way, I get an incorrect result (with maxgrade = 630):
SELECT SUM(a.maxgradepoints) AS maxgrade, AVG(g.earned) AS avgrade FROM assignmentdata a, grades g

If I set it up this way, I get a correct result with (maxgrade = 30):
SELECT SUM(maxgradepoints) AS maxgrade FROM assignmentdata

My table looks like this:

TABLE `assignmentdata` (
  `id` smallint(6) NOT NULL auto_increment,
  `assignmentnum` tinyint(20) NOT NULL,
  `assignmentname` varchar(100) NOT NULL,
  `maxgradepoints` tinyint(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- 
-- Dumping data for table `assignmentdata`
-- 

INSERT INTO `assignmentdata` VALUES (1, 1, 'Photograph Something Meaningful', 10);
INSERT INTO `assignmentdata` VALUES (2, 2, 'Portraits (1 straight, 1 environmental)', 10);
INSERT INTO `assignmentdata` VALUES (3, 3, 'Sports (Action)', 10);

I'm using MySQL 5.0.18.


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 September 2006 - 03:56 PM

Where's your JOIN condition?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 30 September 2006 - 04:30 PM

What is the structure of your  "grades" table?


Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 September 2006 - 09:31 PM

You're just returning too many rows, since you're getting M*N records.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 30 September 2006 - 10:10 PM

I didn't think I needed to use a JOIN here. I am using the data separately in two unrelated areas. Do I need a JOIN anyway?

The grades table is as such:
-- 
-- Table structure for table `grades`
-- 

CREATE TABLE `grades` (
  `id` smallint(6) NOT NULL auto_increment,
  `studentid` smallint(6) NOT NULL,
  `assignmentid` smallint(6) NOT NULL,
  `earned` decimal(3,1) NOT NULL,
  `comment` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
The sort of data I'm inserting:
INSERT INTO `grades` VALUES (7, 1, 1, 9.0, 'Nice solution to a difficult...etc. etc.');

@fenway: Why would the query return so many results? I thought that that query would get me the sum of the one column and the average of the other and they would be unrelated to to each other. Where is the connection between them that is causing the problem?

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 01 October 2006 - 02:31 PM

If you want the SUM from one table, and the AVG from another, and they are truly "unrelated", then you'll have to use two separate queries.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 01 October 2006 - 04:13 PM

Hi,

I guess your query needs to be like this. There needs to be some mapping between the two table (primary-foriegn) key relation.

assignmentdata table values

[tr][td]3, 3, 'Sports (Action)', 10
2, 2, 'Portraits (1 straight, 1 environmental)', 10
1, 1, 'Photograph Something Meaningful', 10


grades table values

[tr][td]1, 1, 1, '9.0', 'Nice solution to a difficult...etc. etc.'
2, 1, 1, '8.0', 'Nice solution to a difficult...etc. etc.'




SELECT SUM(a.maxgradepoints) AS maxgrade, AVG(g.earned) AS avgrade FROM assignmentdata a, grades g where a.id=g.id

Output

[tr][td]20    8.50000


What I would suggest is to have your tables relations fixed. Go through the basics.
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#8 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 01 October 2006 - 05:25 PM

OK, thanks guys. I'll fix their relations as the tables do need to be reconfigured in a few places. Especially since they really aren't truly, totally, unrelated. Much appreciated.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users