Results 1 to 7 of 7
  1. #1
    Mikey1987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Posts
    19

    Query wrong sum

    Hello,

    I got perhaps a silly question, but I can't seem to figure it out.

    I want to do a query on invoices.
    There are 2 tables: a table with the invoiceID, the NumberofHoursWorked and the SalaryPerHour
    The other table contains Material (with a price and the number of items) for each invoice.

    Obviously, an invoice can contain several items, and an item can appear on many invoices.

    The query I want to do is as simple as this: Calculate the total amount of the invoice
    Total Labor cost = NumberOfHoursWorked * SalaryPerHour
    Total Material cost = sum of the cost of all items (item price * number of items)
    Tax: (Total Labor cost + Total Material Cost) * Tax percentage



    That all works fine, but when I do the query, I got the Total Labor Cost for each of the material items
    so InvoiceID 1 with 3 items, would count the Total Labor Cost 3 times

    It probably has something to do with group by and stuff, but it's not working...

    any ideas?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    That would be expected in a query that joins these two tables.

    Consider a report/subreport arrangement.

    Or if you don't care about the details of materials, do an aggregate query that summarizes the material costs by InvoiceID and then join that query to the Invoices table.
    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
    Mikey1987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Posts
    19
    Hello june7

    I indeed don't care about the details. The query will fuel a subform, for which I simply need the total amount.

    How would you build that aggregate query exactly? Do I need a subquery perhaps? All I want is the SUM of all the items * their price, and the hours worked * salary (and the sum of these multiplied with tax %)

    Any advice would be greatly appreciated

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What fields are in the materials table?
    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
    Mikey1987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Posts
    19
    It's actually a junction table between the material table and the invoice table

    There are 3 fields in the junction table: invoiceID, materialID and "Material_Amount".

    The materialID of course relates to items in the materialntable, with among others the price of the item

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    In query builder: join the Materials table to the junction table linking on MaterialID fields. Then click the Totals button on ribbon.

    Group by the invoiceID, do Sums on Material_Amount and Price or Sum on Material_Amount * Price.

    Save that query.

    Now build another query that joins the aggregate query to the Invoices table by linking on InvoiceID fields. Do other calcs in this query.

    The two queries can be a single query if you want to do nested statements in SQL View.
    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.

  7. #7
    Mikey1987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Posts
    19
    Quote Originally Posted by June7 View Post
    In query builder: join the Materials table to the junction table linking on MaterialID fields. Then click the Totals button on ribbon.

    Group by the invoiceID, do Sums on Material_Amount and Price or Sum on Material_Amount * Price.

    Save that query.

    Now build another query that joins the aggregate query to the Invoices table by linking on InvoiceID fields. Do other calcs in this query.

    The two queries can be a single query if you want to do nested statements in SQL View.
    That worked like a charm... awesome, thx mate!

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

Similar Threads

  1. Whats wrong with my query
    By Kirtap in forum Queries
    Replies: 5
    Last Post: 10-01-2013, 10:31 AM
  2. What's wrong with this Query?
    By Richie27 in forum Queries
    Replies: 3
    Last Post: 05-25-2012, 08:25 AM
  3. Please help: what is wrong with this query?
    By drhassan in forum Queries
    Replies: 4
    Last Post: 05-24-2012, 05:15 PM
  4. What's wrong with my query?
    By Grek in forum Queries
    Replies: 2
    Last Post: 10-31-2011, 12:12 PM
  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