Results 1 to 2 of 2
  1. #1
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31

    Help! Access SQL Query

    I have a question about aggregating numbers.

    Suppose I have a SALES table that whose records are at an item transaction level (i.e. it displays sales by item).

    I also have an ITEM table that contains item dept_nbr and sub_cat_nbr.


    There is a DEPT table that maps dept_nbr to dept_desc, and a SUB_CAT table that maps sub_cat_nbr to sub_cat_desc.

    Question: Suppose I want to find the total sales listed by department and subcategory descriptions. How can I best write the SQL? I don't want to see sales at the item level, only on the department and subcategory levels.

    SALES table columns: sales_nbr, item_nbr, sales$
    ITEM table columns: item_nbr, dept_nbr, sub_cat_nbr
    DEPT table columns: dept_nbr, dept_desc
    SUB_CAT table columns: sub_cat_nbr, sub_cat_desc

    Would this work?

    SELECT distinct
    dept.dept_desc,
    sub_cat.sub_cat_desc,
    SUM(sales.sales$)
    FROM
    dept, sub_cat, item, sales
    WHERE
    dept.dept_nbr = item.dept_nbr
    AND sub_cat.sub_cat_nbr = item.sub_cat_nbr
    AND item.item_nbr = sales.item_nbr

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    What I would do first is create a query that gets the detailed information; something like this:

    query name: qrySalesDetail
    SELECT tblSales.sales_nbr, tblSales.SalesAmt, tblDept.dept_nbr, tblDept.dept_desc, tblSubCat.sub_cat_nbr, tblSubCat.sub_cat_desc
    FROM tblSubCat INNER JOIN ((tblDept INNER JOIN tblItems ON tblDept.pkDeptID = tblItems.fkDeptID) INNER JOIN tblSales ON tblItems.pkItemID = tblSales.fkItemID) ON tblSubCat.pkSubCatID = tblItems.fkSubCatID;


    Then use the above query in another query that sums the sales by dept and subcat.

    query name: qrySalesSummaryByDeptSubCat
    SELECT qrySalesDetail.dept_desc, qrySalesDetail.sub_cat_desc, Sum(qrySalesDetail.SalesAmt) AS SumOfSalesAmt
    FROM qrySalesDetail
    GROUP BY qrySalesDetail.dept_desc, qrySalesDetail.sub_cat_desc;


    I was not sure if the number fields in your tables were key fields or not.

    I have attached a sample database that I created that includes the pertinent tables and the queries mentioned above. Hopefully you will be able to adapt the example to your database.

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

Similar Threads

  1. Help with Access Query
    By dae2604 in forum Access
    Replies: 2
    Last Post: 10-21-2010, 07:23 AM
  2. SQL Query in Access
    By DaveyJ in forum Queries
    Replies: 1
    Last Post: 06-17-2010, 12:26 PM
  3. Access Query
    By malaysarkar in forum Queries
    Replies: 0
    Last Post: 11-26-2009, 12:57 PM
  4. Sql server query to MS access query
    By blazixinfo@yahoo.com in forum Queries
    Replies: 0
    Last Post: 07-07-2009, 08:12 AM
  5. Access Query
    By LinneaUD13 in forum Queries
    Replies: 0
    Last Post: 06-05-2009, 03:46 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