Jump to content

Archived

This topic is now archived and is closed to further replies.

Gaoshan

A SELECT using SUM and AVG that returns the wrong result

Recommended Posts

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):
[code]SELECT SUM(a.maxgradepoints) AS maxgrade, AVG(g.earned) AS avgrade FROM assignmentdata a, grades g[/code]

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

My table looks like this:

[code]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);[/code]

I'm using MySQL 5.0.18.

Share this post


Link to post
Share on other sites
Where's your JOIN condition?

Share this post


Link to post
Share on other sites
You're just returning too many rows, since you're getting M*N records.

Share this post


Link to post
Share on other sites
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:
[code]--
-- 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;
[/code]
The sort of data I'm inserting:
[code]INSERT INTO `grades` VALUES (7, 1, 1, 9.0, 'Nice solution to a difficult...etc. etc.');
[/code]

@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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hi,

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

[b]assignmentdata table values[/b]

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

[b]grades table values[/b]

[table][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.'[/table]



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

[b]Output[/b]

[table][tr][td]20    8.50000[/table]

What I would suggest is to have your tables relations fixed. Go through the basics.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.