Jump to content

A SELECT using SUM and AVG that returns the wrong result


Gaoshan

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.