Gaoshan Posted September 29, 2006 Share Posted September 29, 2006 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. Quote Link to comment Share on other sites More sharing options...
fenway Posted September 30, 2006 Share Posted September 30, 2006 Where's your JOIN condition? Quote Link to comment Share on other sites More sharing options...
shivabharat Posted September 30, 2006 Share Posted September 30, 2006 What is the structure of your "grades" table? Quote Link to comment Share on other sites More sharing options...
fenway Posted September 30, 2006 Share Posted September 30, 2006 You're just returning too many rows, since you're getting M*N records. Quote Link to comment Share on other sites More sharing options...
Gaoshan Posted September 30, 2006 Author Share Posted September 30, 2006 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? Quote Link to comment Share on other sites More sharing options...
fenway Posted October 1, 2006 Share Posted October 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
shivabharat Posted October 1, 2006 Share Posted October 1, 2006 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)', 102, 2, 'Portraits (1 straight, 1 environmental)', 101, 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. Quote Link to comment Share on other sites More sharing options...
Gaoshan Posted October 1, 2006 Author Share Posted October 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.