Results 1 to 12 of 12
  1. #1
    bugme is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    15

    New to queries

    Hi all,

    I'm new to Access, but I have a database which I'm having trouble trying to get a simplified query from. The databases I currently have are below:

    Sales database:
    Date Product
    Colour
    Quantity Sold
    01/01/2015 T-shirt Blue 3
    02/01/2015 T-shirt Black 6
    04/01/2015 Pants Red 2
    04/01/2015 Pants Green 1
    05/01/2015 T-shirt Blue 2
    07/01/2015 Pants Green 1

    Stock on Hand (SOH) Database:
    Product
    Colour
    SOH as at end of 31/12/2014


    T-shirt Blue 10
    T-shirt Black 12
    Pants Red 6
    Pants Green 5


    What I'm trying to do is to output a query which sums up the total sales and current inventory of each product (irregardless of colour). I've written down what the query should look like, but I'm not sure how to get there. Any ideas?

    Product
    SOH at end of 2014 Quantity Sold
    Current SOH
    T-shirt 22 11 11
    Pants 11 4 7
    Last edited by bugme; 04-03-2015 at 05:45 AM.

  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    use a group by query - group by product and sum the other columns

  3. #3
    bugme is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    15
    Thanks a lot for the reply mate. I've updated the question in the OP as it's a little more complicated than what I thought it will be.

  4. #4
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    same answer as before - just don't bring through the date or colour

  5. #5
    bugme is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    15
    For some reason, this is the result I am getting (I've omitted the Current SOH for now):

    Product
    SOH at end of 2014 Quantity Sold
    T-shirt 66 22
    Pants 33 8

    The two latter columns are grouped by 'Sum' and the Product column is just left as 'Group By'.

  6. #6
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    Ah - it is because you have 3 sales records and 2 SOH records - so SOH is repeated 3 times - and added 3 times.

    You could try changing sum to avg or to be clearer you need two group by queries, one on the Sales table and one of the SOH table which you then combine in a third query, linking on product.

    However it does get more complicated because you might have sales in the year, but no SOH (e.g. a new product), or you might have a SOH, but no sales. In either of these cases, a result will not appear.

    To resolve this you need to bring in the product table as well and left join it to the two group by queries - so you will see all products and a SOH where it exists and sales where they exist

  7. #7
    bugme is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    15
    Quote Originally Posted by Ajax View Post
    Ah - it is because you have 3 sales records and 2 SOH records - so SOH is repeated 3 times - and added 3 times.

    You could try changing sum to avg or to be clearer you need two group by queries, one on the Sales table and one of the SOH table which you then combine in a third query, linking on product.

    However it does get more complicated because you might have sales in the year, but no SOH (e.g. a new product), or you might have a SOH, but no sales. In either of these cases, a result will not appear.

    To resolve this you need to bring in the product table as well and left join it to the two group by queries - so you will see all products and a SOH where it exists and sales where they exist
    I've uploaded the Access file as for some reason, I just can't figure it out. Are you able to look at it for me?
    Attached Files Attached Files

  8. #8
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    One of the reason you are having problems is your data isn't normalised. I've taken the liberty of normalising it - please see attached which also has the solution you are looking for
    Attached Files Attached Files

  9. #9
    bugme is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    15
    You're a lifesaver! The database I have doesn't have separate tables for 'products' and 'product colours', but I managed to get it to work. Thanks again!

  10. #10
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    The database I have doesn't have separate tables for 'products' and 'product colours'
    It should do if you are doing it properly!

  11. #11
    bugme is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2011
    Posts
    15
    I'm trying to minimise the filesize of this database (the wonders of working with big data) and am looking at a way to perform this query without having to use subqueries as above. My instinct says to use a SQL query to achieve this - any suggestions?

  12. #12
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    you'll need to remind me of the query. Also not sure what you mean by using a sql query - all queries are sql

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

Similar Threads

  1. DB queries
    By hinkwale in forum Access
    Replies: 1
    Last Post: 12-30-2014, 04:39 PM
  2. Replies: 9
    Last Post: 08-27-2014, 11:34 AM
  3. Replies: 6
    Last Post: 11-13-2013, 04:17 PM
  4. Using Sub Queries?
    By djclntn in forum Queries
    Replies: 4
    Last Post: 04-05-2012, 08:58 AM
  5. Need Help with Queries
    By mulefeathers in forum Queries
    Replies: 2
    Last Post: 05-24-2010, 11:37 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