Results 1 to 3 of 3
  1. #1
    PBE is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    2

    SQL to show sales which are higher then average sales by branch

    Hi Any help appreciated.

    I'm trying to write a query that shows BranchName, Sales and SalesDate for records where the Sales amount >= to the average sales amount for the branch ie. the average sales for Brighton is 237 so the record for Brighton where sales are 200 should be excluded.

    To illustrate what I want I have pasted tables with basic data.

    BranchID BranchName
    1 Brighton
    2 Crawley
    3 Worthing
    4 London
    5 Eastbourne

    BranchID SaleDate Sales
    1 2006-09-15 200
    1 2006-10-13 300
    1 2006-11-02 150
    1 2007-04-01 300
    2 2006-11-03 4000
    3 2005-01-01 600
    3 2005-02-02 900
    3 2005-03-15 700
    3 2006-05-16 900
    5 2006-10-01 775


    This code shows only branches whose average sales were greater than or equal to 300.

    Code:
    SELECT DISTINCTROW tbl_Branch.BranchName, Avg(tbl_BranchSales.Sales) AS AverageSales
    FROM tbl_Branch INNER JOIN tbl_BranchSales ON tbl_Branch.[BranchID] = tbl_BranchSales.[BranchID]
    GROUP BY tbl_Branch.BranchName
    HAVING (((Avg(tbl_BranchSales.Sales))>=300));
    How do I modify this code to show:-



    BranchName, Sales and SalesDate for records where the Sales amount >= to the average sales amount for the branch ie. the average sales for Brighton is 237 so the record for Brighton where sales are 200 should be excluded.

    Thanks

    PBE

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,913
    Build a query that calculates the average by Branch then join that query to Sales 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
    PBE is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    2
    Thank you for this. I now have it working!!

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

Similar Threads

  1. Replies: 1
    Last Post: 03-07-2019, 11:58 AM
  2. Replies: 2
    Last Post: 11-14-2018, 08:20 AM
  3. Replies: 16
    Last Post: 12-28-2017, 02:04 PM
  4. Replies: 26
    Last Post: 11-02-2016, 10:38 AM
  5. Replies: 0
    Last Post: 12-13-2012, 03:18 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