Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / March 2008

Tip: Looking for answers? Try searching our database.

Inner Join query generating wrong result

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sahafi - 12 Mar 2008 20:14 GMT
My query is based on 2 tables (pounds & Hours). Both tables use autonumber as
a PK. Both tables share 5 similar fields:(Year, Month, Week, Location,
ProdCategory) but as you can see none of those fields is unique. The pound
table has additional field (ProdCode) by which the pounds are grouped.
In the query I need to show only the Year (no month/week), Location,
ProdCategory, and SumOfPounds form the pound table, plus the (Hours) from the
hours table. When I group all 4 fields and sum the pounds field, I get a
large data (as if i'm pulling weekly data). When I sum the hours as well as
the pounds I get a wrong result (way larger numbers for both fields than it
shoud be).
How can I get sum of pounds/hours for each ProdCategory at each Location for
the year as one number?

Thanks.
Signature

when u change the way u look @ things, the things u look at change.

John Spencer - 13 Mar 2008 12:50 GMT
I would suggest a query with sub-queries as the source, but I am fearful
that it wouldn't work with your field names

SELECT Year, Location, ProdCategory, Sum(Pounds) asTotalPounds
FROM Pounds
GROUP BY Year, Location, ProdCategory

SELECT Year, Location, ProdCategory, Sum(Hours) asTotalHours
FROM Hours
GROUP BY Year, Location

Then using those two queries

SELECT P.Year,  P.Location, P.ProdCategory
, P.TotalPounds, H.TotalHours
FROM QueryOne as P INNER JOIN QueryTwo as H
On P.Year = H.Year
and P.Location = H.Location
and P.ProdCategory = H.ProdCategory

All in one if your field names and table names consist of only letters,
numbers, and underscore characters might be as follows.

SELECT P.Year,  P.Location, P.ProdCategory
, P.TotalPounds, H.TotalHours
FROM (
   SELECT Pounds.Year, Pounds.Location, Pounds.ProdCategory
   , Sum(Pounds.Pounds) asTotalPounds
    FROM Pounds
   GROUP BY Pounds.Year, Pounds.Location, Pounds.ProdCategory
    ) as P INNER JOIN
   (
   SELECT Hours.Year, Hours.Location, Hours.ProdCategory
   , Sum(Hours.Hours) asTotalHours
   FROM Hours
  GROUP BY Hours.Year, Hours.Location
   ) as H
On P.Year = H.Year
and P.Location = H.Location
and P.ProdCategory = H.ProdCategory

Signature

John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
.

> My query is based on 2 tables (pounds & Hours). Both tables use autonumber
> as
[quoted text clipped - 15 lines]
>
> Thanks.
sahafi - 13 Mar 2008 15:49 GMT
John,
I used the first option, and it works perfectly. Thank you so much.

Os.

> I would suggest a query with sub-queries as the source, but I am fearful
> that it wouldn't work with your field names
[quoted text clipped - 56 lines]
> >
> > Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.