I have a series of tables as below:
CREATE TABLE `collectorfeetable` (
`colFeeID` int(10) unsigned NOT NULL default '0',
`colFeeAmount` decimal(10,2) NOT NULL default '0.00',
`colTimeStamp` datetime default NULL,
`colWorklist` char(4) NOT NULL default '',
PRIMARY KEY (`colFeeID`),
KEY `feeamounts` (`colFeeAmount`),
KEY `feesandworklist` (`colFeeAmount`,`colWorklist`),
KEY `worklistandfee` (`colWorklist`,`colFeeAmount`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 PACK_KEYS=1
CREATE TABLE `collectorworklists` (
`colWorklistxref` int(10) unsigned NOT NULL auto_increment,
`collectorID` char(4) NOT NULL default '',
`collectorWorklist` char(5) character set utf8 NOT NULL default '',
`collectorWorklistLastTimeStamp` datetime NOT NULL default
'0000-00-00 00:00:00',
`collectorWorklistCallSheet` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`colWorklistxref`),
KEY `Worklist` (`collectorWorklist`)
) ENGINE=MyISAM AUTO_INCREMENT=61 DEFAULT CHARSET=latin1 PACK_KEYS=1
CREATE TABLE `collectordailygoals` (
`collectorID` char(4) NOT NULL default '',
`GoalDAY` tinyint(3) unsigned NOT NULL default '0',
`PromiseGoal` smallint(5) unsigned default NULL,
`WorkedGoal` smallint(5) unsigned default NULL,
`goalUpdate` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`collectorID`,`GoalDAY`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 PACK_KEYS=1
The problem query is:
SELECT sum(cf.colfeeamount) , collectordailygoals.collectorid from
collectorfeetable cf inner join collectorworklists cw on
cw.collectorworklist = cf.colworklist inner join collectordailygoals on
collectordailygoals.collectorid = cw.collectorid where
month(cf.coltimestamp) = month(current_date()) and year(cf.coltimestamp)
= year(current_date()) group by collectordailygoals.collectorid order by
collectorid ;
My problem is that the sum is multiplied by the number of times a
collector ID is present in the collectordailygoals table. How do I set
up the join such that it looks at only unique instances of the collector
ID in the collectordailygoals table? Alternatively how would I divide
the sum by the number of instances that the collector ID shows up in the
collectordailygoals table? Despite the presence of the goalupdate
timestamp this particular table does not hold historical data. That is
kept in another table.
When I get a chance I will look into overhauling the database structure.
Until then I am stuck with what you see.
Thanks.