Results 1 to 9 of 9
  1. #1
    eliash is offline Novice
    Windows XP Access 2010 (version 14.0)
    Join Date
    May 2010
    Posts
    6

    Summing values from different tables, grouped by values from different tables.

    If I have four tables:
    Products
    Purchases
    Sales
    Destruction



    The three latter tables refer to products from the first table (often the same product many times), using the same field name "product", and each have a column which tells how much of the product is treated. These columns have a different name for each table, respectively: "delivered", "ordered", "destroyed"

    What I want to do is to generate the stock levels from these tables. The idea is to take the number of purchased items, grouped by product, and then subtract the sold and destroyed items, each grouped by their own product fields. (I would also be interested in how to write a general function of the three values for each product.)

    The end result is a table where each row uniquely represents a product and has a field which shows the current stock level. I believe this is very possible and a favorable solution to auto-editing a static table representing the inventory. Please correct me if I am wrong, because I am rather new to Access.

    Thank you.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    One solution would be to create 3 totals queries on the 3 transaction tables. Each would group on product and sum the quantity. Then create a 4th query that includes the product table with a LEFT JOIN to each of those 3 queries. That will get you the quantities from each table, whether there's been activity or not.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    eliash is offline Novice
    Windows XP Access 2010 (version 14.0)
    Join Date
    May 2010
    Posts
    6
    Yes, this sounds like what I am looking for. I have managed to create the three totals queries, and tried to write a LEFT JOIN, but the syntax (or Access's interpretation of it, possibly?) troubles me. When you say "LEFT JOIN", would that be an inner or an outer join?

    I would greatly appreciate if you could provide the code for this example (assuming the existence of the three totals queries, with the same name for the totals column in each of them).

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It would be an outer, but in Access you can simply say

    FROM ProductTable LEFT JOIN FirstQuery ON ...

    You can also edit the default joins in query design view by right-clicking on the join lines and choosing the appropriate selection. Drop all 4 into a new query in design view, create your joins by clicking and dragging, then edit the joins. To be honest, I get confused as to how Access parenthesizes the joins sometimes, so that's how I would do it myself.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    eliash is offline Novice
    Windows XP Access 2010 (version 14.0)
    Join Date
    May 2010
    Posts
    6

    Question

    EDIT: SOLVED

    Thank you, but I am not sure I follow. This left outer join, will it somehow sum the values, as I requested, or will it simply list them or a subset of them?
    Last edited by eliash; 05-20-2010 at 05:38 AM.

  6. #6
    eliash is offline Novice
    Windows XP Access 2010 (version 14.0)
    Join Date
    May 2010
    Posts
    6
    EDIT: SOLVED

    After failing with LEFT JOINs I have tried a new method. The following code achieves what I requested, using the easier to read UNION operation:

    SELECT Product, Sum(Total) AS Stock
    FROM (SELECT ALL Product, Received AS Total
    FROM Purchases
    UNION SELECT ALL Product, (-1)*Ordered AS Total
    FROM Sales
    UNION SELECT ALL Product, (-1)*Destroyed AS Total
    FROM Destruction)
    GROUP BY Product;

    It does, however, not list the Products by name, but by ID number. In order to solve this, I tried the following:

    SELECT p.ProductID, p.Productname AS Product, Stock
    FROM Products AS p LEFT JOIN
    (SELECT Product, Sum(Total) AS Stock
    FROM (SELECT ALL Product AS ProductID, Received AS Total
    FROM Purchases
    UNION SELECT ALL Product AS ProductID, (-1)*Ordered AS Total
    FROM Sales
    UNION SELECT ALL Product AS ProductID, (-1)*Destroyed AS Total
    FROM Destruction)
    GROUP BY Product) AS q
    ON p.ProductID=q.ProductID;

    This returns the following error message:
    "The inclusion of constant expressions in an outer join operation is not supported."

    I have found nothing about this error message on the web. Can anyone see where the problem is, or point it out if this depends on more information about the particular tables?

  7. #7
    eliash is offline Novice
    Windows XP Access 2010 (version 14.0)
    Join Date
    May 2010
    Posts
    6
    I solved it. Thanks for helping me keep trying. ;-)

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    And all that while I slept!

    Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    eliash is offline Novice
    Windows XP Access 2010 (version 14.0)
    Join Date
    May 2010
    Posts
    6
    I think I owe to this forum to post the solution:

    SELECT p.Productname AS Product, u.Stock
    FROM Products AS p LEFT JOIN
    (SELECT l.ProductID AS ProductID, Sum(Total) AS Stock
    FROM
    (SELECT ALL Product AS ProductID, Received AS Total
    FROM Purchases
    UNION SELECT ALL Product AS ProductID, (-1)*Ordered AS Total
    FROM Sales AS s WHERE s.Ordered>0
    UNION SELECT ALL Product AS ProductID, (-1)*Destroyed AS Total
    FROM Destruction) AS l
    GROUP BY l.ProductID) AS q
    ON p.ProductID=q.ProductID;

    There may be typographical errors from my translation, but the principle should be right (or at least seems to work all right for me).

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

Similar Threads

  1. Summing values in report view
    By BigBear in forum Reports
    Replies: 2
    Last Post: 04-06-2010, 04:37 AM
  2. UNION two tables with same primary key values
    By carillonator in forum Queries
    Replies: 1
    Last Post: 02-02-2010, 08:54 PM
  3. Grouped Tables
    By tmcrouse in forum Queries
    Replies: 0
    Last Post: 09-24-2009, 07:10 AM
  4. lookup values in backend tables
    By deb56 in forum Database Design
    Replies: 1
    Last Post: 01-23-2008, 11:12 AM
  5. Replies: 1
    Last Post: 06-20-2007, 07:26 PM

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