Results 1 to 6 of 6
  1. #1
    rbf is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    3

    Sum Query Delivers wrong results

    I'm pretty new to access, but my boss wants me to try and consolidate our massive folder of monthly excell data into a database. So far, I have a few different tables with production data in them. I'm trying to get a daily total (I don't even know if this is the best way to do it) using a sum query. I started by writing a separate query for each table, and it worked fine. When I combine them, the numbers are totally wrong. 10 to 50 x larger than they should be.

    Here is the original query, which works fine:

    SELECT Bench.Date, Sum(Bench.Total_ft) AS Bench_Feet
    FROM Bench
    GROUP BY Bench.Date

    There are three of these, which all work. This is the combined query:

    SELECT Daily_TG.Date, NZ(Sum(Bench.Total_ft),0) AS Bench_Feet, NZ(Sum([Holes_L]+[Holes_R]),0) AS Jumbo_Holes, NZ(Sum(Bolters.Bolts_8),0) AS 8_ft_bolts
    FROM Bench, Jumbos, Bolters, Daily_TG
    GROUP BY Daily_TG.Date;


    Note: Daily_TG.Date is tied in a one to many relationship with the date field in the other 3 tables.



    Any thoughts? I've been fumbling through this for 3 days now.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    There is no JOIN clause in that query. This results in Cartesian joining, every record of every table will join to every record of other tables.

    Do all tables have the same dates? If so, pick one table to be the 'primary' and join the other 3 to it. If tables have different dates, will need a dataset that has all possible dates then join the 4 summary queries to that master dataset. The master can be created by a UNION query. There is no wizard or designer for UNION, must type or copy/paste in SQL View.

    SELECT [Date] FROM Daily_TG
    UNION SELECT [Date] FROM Bench
    UNION SELECT [Date] FROM Jumbos
    UNION SELECT [Date] FROM Bolters;


    BTW, Date is a reserved word, should not use reserved words as names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    rbf is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    3
    Daily_TG has one record for each date. I that is the primary key. I have renamed each field from "Date" to "Day". There will be multiple records for each day in each of the 3 tables (Bolters, Jumbos, Bench).

    I have the inner join working for one table (bolters), I'm just not sure how to word it in order to get all of the tables joined by the day fields.

    SELECT Daily_TG.Day, Nz(Sum(Bench.Total_ft),0) AS Bench_Feet, Nz(Sum([Holes_L]+[Holes_R]),0) AS Jumbo_Holes, Nz(Sum(Bolters.Bolts_8),0) AS 8_ft_bolts
    FROM Daily_TG INNER JOIN Bolters ON Daily_TG.Day = Bolters.Day
    GROUP BY Daily_TG.Day

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Query joining multiple 1-to-many tables will not work. The aggregates must result in one record for each day.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    rbf is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    3
    Ok. Is the best way to do this to write three separate join / sum queries, appending the results to three separate tables, then one query that simply pulls the records from that table into a daily summary?

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Appending to tables not necessary. From my post#2: join the 4 summary queries to that master dataset
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calculation in Query giving wrong results
    By dargo72 in forum Queries
    Replies: 11
    Last Post: 11-07-2012, 05:39 AM
  2. Replies: 6
    Last Post: 05-14-2012, 07:24 AM
  3. Replies: 2
    Last Post: 03-02-2011, 01:43 PM
  4. Expression gives wrong results
    By newtoAccess in forum Queries
    Replies: 22
    Last Post: 12-03-2010, 12:21 AM
  5. What's wrong with this query?
    By jsoldi in forum Queries
    Replies: 2
    Last Post: 10-11-2010, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums