473,406 Members | 2,281 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,406 software developers and data experts.

sum vs count - join problem?

AccessIdiot
493 256MB
I have three tables used in a query.

Table A: Has field with name of species.
Table B: Has the number of specimens caught per catch.
Table C: Has measurements of specimens.

I have generated the query in design view to get the min, max, and avg length of two particular species. The query should also report the number of specimens. For some reason I am not getting the right numbers.

For example, I know for a fact that there were 84 sturgeon. If I use SUM(SpecimenCount) I get a total of 240? But if I use COUNT(SpecimenCount) I get 84.

However, in a simpler query that just reports name of species and SUM(SpecimenCount) it reports the correct number of 84.

My buddy suggested it might have something to do with the kind of join Access is using. When I look at the query in SQL view it is using an Inner Join and looks like this (sorry for the long query)

Expand|Select|Wrap|Line Numbers
  1. SELECT tbl_Species_Freshwater.Common_Name, Min(tbl_Specimen_Fish_Replicate.Total_Len) AS MinOfTotal_Len, Max(tbl_Specimen_Fish_Replicate.Total_Len) AS MaxOfTotal_Len, Avg(tbl_Specimen_Fish_Replicate.Total_Len) AS AvgOfTotal_Len, Min(tbl_Specimen_Fish_Replicate.Standard_Len) AS MinOfStandard_Len, Max(tbl_Specimen_Fish_Replicate.Standard_Len) AS MaxOfStandard_Len, Avg(tbl_Specimen_Fish_Replicate.Standard_Len) AS AvgOfStandard_Len, Sum(tbl_Specimen_Replicate.SpecimenCount) AS SumOfSpecimenCount
  2. FROM tbl_Species_Freshwater INNER JOIN (tbl_Specimen_Replicate INNER JOIN tbl_Specimen_Fish_Replicate ON tbl_Specimen_Replicate.Specimen_ID = tbl_Specimen_Fish_Replicate.Specimen_ID) ON tbl_Species_Freshwater.Species_ID = tbl_Specimen_Replicate.Species_ID
  3. GROUP BY tbl_Species_Freshwater.Common_Name, tbl_Species_Freshwater.Species_Code
  4. HAVING (((tbl_Species_Freshwater.Species_Code)="GRNSTURG" Or (tbl_Species_Freshwater.Species_Code)="WHTSTURG"))
  5. ORDER BY tbl_Species_Freshwater.Common_Name;
I have no idea what kind of join I should be using instead. I just know that the correct number of sturgeon in the db is 84. :)

Any ideas?
Aug 13 '07 #1
4 2469
AccessIdiot
493 256MB
Okay, I might have found a solution. Instead of trying to do a sum on the SpecimenCount field I just did a count on the total length field - in other words, counting how many records held a total length. I think that will work just fine.

:)
Aug 13 '07 #2
Rabbit
12,516 Expert Mod 8TB
The problem is the multiple joins.

What's going on is:

Expand|Select|Wrap|Line Numbers
  1. Table1
  2. MainID(PK)
  3. 1
  4. 2
  5. 3
  6.  
  7. Table2
  8. SecID(PK)     MainID(FK)
  9. 1             1
  10. 2             1
  11. 3             2
  12. 4             2
  13. 5             3
  14. 6             3
  15.  
  16. Table3
  17. TerID(PK)     SecID(FK)
  18. 1             1
  19. 2             1
  20. 3             2
  21. 4             2
  22. 5             3
  23. 6             3
  24. 7             4
  25. 8             4
  26. 9             5
  27. 10            5
  28. 11            6
  29. 12            6
  30.  
When you join Table2 and Table3, you get:
Expand|Select|Wrap|Line Numbers
  1. TerID     Table2.MainID     Table2.SecID=Table3.SecID
  2. 1         1                 1
  3. 2         1                 1
  4. 3         1                 2
  5. 4         1                 2
  6. 5         2                 3
  7. 6         2                 3
  8. 7         2                 4
  9. 8         2                 4
  10. 9         3                 5
  11. 10        3                 5
  12. 11        3                 6
  13. 12        3                 6
  14.  
So, as you can see, if you go to sum/count anything in Table2, it's going to be more than it's supposed to be because its records get multiplied by the one-to-many relationship. In this case, any count done on Table2 get's mutliplied by 2. But only because I gave each record in Table2 2 records in Table3. So depending on how many duplicate foreign keys you have, the more your count/sum gets multiplied.

And the problem is even greater if you do a sum/count on Table1.

You'll have to seperate the sum into another query.
Aug 13 '07 #3
AccessIdiot
493 256MB
Ah, that makes sense. Well I'm certainly glad I found a work around!

Thanks for the explanation
Aug 13 '07 #4
Rabbit
12,516 Expert Mod 8TB
Not a problem, good luck.
Aug 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: TGOS | last post by:
Here's a little question to MySQL Gurus. Two tables. One called 'document' one called 'page'. Needles to say, a document consist out of multiple pages (1:n). The primary (P) and foreign (F) keys...
1
by: Jenny | last post by:
Hi all, Hope someone can help me with this ... This is my query. The problem with it is that it only returns Areas with >0 Topics. So if a new Area is created, it hasn't a topic until one is...
6
by: Good Man | last post by:
Hi all Well, I didn't want to have to bring this question to someone else to answer, but I am truly flummoxed and could use some help. It all boils down to trying to find the number of jobs...
2
by: Julio Allegue | last post by:
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It returns all the rows. It doesn't seem to look at the WHERE clause. At the same time, I am getting the correct count on "SQL...
4
by: Igor | last post by:
I have one SELECT statement that needs to return one ntext field from one table and count something from other table, problem is that all fileds that are not in count have to be in group by and...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
5
by: whitsey | last post by:
Here is what I have: SELECT (SELECT COUNT(*) AS SEARCHES FROM SEARCHES INNER JOIN GROUPS ON SEARCHES.SITE_ID = GROUPS.SITE_ID WHERE
2
by: Alo | last post by:
I have a query that is counting records of a certain type in another table, however, when the count is 0, the join type no longer exists either. How do I get it to return a count of 0 if there is no...
1
by: muld | last post by:
I've inherited a database system designed to calculate sickness absence statistics. There is a main table with an employee ID which also holds the number of days worked by that person in a year ....
4
by: vincibleman | last post by:
Howdy all, Working my way into SQL from Access. Think I might have the hang of the basics, but would really appreciate a sanity check. The stored procedure listed below works, but I can't help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.