473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unwanted Multiplication Due to Join

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.
Aug 30 '06 #1
3 2632
I forgot to mention that this is with 5.0.24 (not 5.0.24a yet) on
Windows 2003. Oops.

No bother wrote:
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.
Sep 1 '06 #2
No bother wrote:
I forgot to mention that this is with 5.0.24 (not 5.0.24a yet) on
Windows 2003. Oops.
Thanks for specifying the version!

Okay, to solve your duplication problem, how about eliminating the
collectordailygoals from the query?

SELECT SUM(cf.colfeeamount), cw.collectorid
FROM collectorfeetable cf
INNER JOIN collectorworklists cw
ON cw.collectorworklist = cf.colworklist
WHERE MONTH(cf.coltimestamp) = MONTH(CURRENT_DATE())
AND YEAR(cf.coltimestamp) = YEAR(CURRENT_DATE())
GROUP BY cw.collectorid
ORDER BY cw.collectorid;

collectordailygoals doesn't seem to be doing anything in your original
query, unless there are fewer collector id's in that table than in
collectorworklists, and you need the join to pair down the list. If
that's the case, you can use a subquery instead of a JOIN to avoid the
duplication.

SELECT SUM(cf.colfeeamount), cw.collectorid
FROM collectorfeetable cf
INNER JOIN collectorworklists cw
ON cw.collectorworklist = cf.colworklist
WHERE MONTH(cf.coltimestamp) = MONTH(CURRENT_DATE())
AND YEAR(cf.coltimestamp) = YEAR(CURRENT_DATE())
AND cw.collectorid IN (SELECT cd.collectorid FROM collectordailygoals cd)
GROUP BY cw.collectorid
ORDER BY cw.collectorid;

Regards,
Bill K.
Sep 1 '06 #3
I kept the query in my question sufficiently simple to illustrate the
problem. I will be pulling data from collectordailygoals but did not
use it in the example query so as to avoid confusion. Thanks for the help!
Bill Karwin wrote:
No bother wrote:
>I forgot to mention that this is with 5.0.24 (not 5.0.24a yet) on
Windows 2003. Oops.

Thanks for specifying the version!

Okay, to solve your duplication problem, how about eliminating the
collectordailygoals from the query?

SELECT SUM(cf.colfeeamount), cw.collectorid
FROM collectorfeetable cf
INNER JOIN collectorworklists cw
ON cw.collectorworklist = cf.colworklist
WHERE MONTH(cf.coltimestamp) = MONTH(CURRENT_DATE())
AND YEAR(cf.coltimestamp) = YEAR(CURRENT_DATE())
GROUP BY cw.collectorid
ORDER BY cw.collectorid;

collectordailygoals doesn't seem to be doing anything in your original
query, unless there are fewer collector id's in that table than in
collectorworklists, and you need the join to pair down the list. If
that's the case, you can use a subquery instead of a JOIN to avoid the
duplication.

SELECT SUM(cf.colfeeamount), cw.collectorid
FROM collectorfeetable cf
INNER JOIN collectorworklists cw
ON cw.collectorworklist = cf.colworklist
WHERE MONTH(cf.coltimestamp) = MONTH(CURRENT_DATE())
AND YEAR(cf.coltimestamp) = YEAR(CURRENT_DATE())
AND cw.collectorid IN (SELECT cd.collectorid FROM collectordailygoals cd)
GROUP BY cw.collectorid
ORDER BY cw.collectorid;

Regards,
Bill K.
Sep 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Soeren Sonnenburg | last post by:
Hi all, Just having started with python, I feel that simple array operations '*' and '+' don't do multiplication/addition but instead extend/join an array: a= >>> b= >>> a+b
2
by: allyn44 | last post by:
Hello--I have 2 tables (illness,event) that a need to query and create a recordset The key fields are personId and description (text field) in each table. I also have other needed fields in the...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
7
by: Edward Elliott | last post by:
I'm looking for the "best" way to strip a large set of chars from a filename string (my definition of best usually means succinct and readable). I only want to allow alphanumeric chars, dashes,...
5
by: Don Do | last post by:
Ok, this is my 2nd ever post. 1st one was solved quickly. Thanks. Now I'm stuck again. My mdb has a main form with 3 subforms. Each tied to the succeeding by single autonumberID/foreign...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.