SQL Access Query

PBE

New Member
Joined
Dec 15, 2019
Messages
2
Office Version
365
Platform
Windows
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.

BranchIDBranchName
1Brighton
2Crawley
3Worthing
4London
5Eastbourne

BranchIDSaleDateSales
12006-09-15200
12006-10-13300
12006-11-02150
12007-04-01300
22006-11-034000
32005-01-01600
32005-02-02900
32005-03-15700
32006-05-16900
52006-10-01775


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
 

hernantorres23

Active Member
Joined
Nov 21, 2019
Messages
256
Office Version
365, 2016
Platform
Windows, Web
Hi, you can try this

SELECT tblSales.branchId, tblCities.branchName, tblSales.salesDate, tblSales.sales, DAvg("sales","tblsales","tblsales.branchid=" & [tblcities].[branchid]) AS promedio
FROM tblSales INNER JOIN tblCities ON tblSales.branchId = tblCities.branchId
GROUP BY tblSales.branchId, tblCities.branchName, tblSales.salesDate, tblSales.sales, DAvg("sales","tblsales","tblsales.branchid=" & [tblcities].[branchid])
HAVING tblSales.sales>=DAvg("sales","tblsales","tblsales.branchid=" & [tblcities].[branchid])


promedio

branchIdbranchNamesalesDatesalespromedio
1​
Brighton
13/10/2006​
300​
237.5
1​
Brighton
01/04/2007​
300​
237.5
2​
Crawley
03/11/2006​
4000​
4000
3​
Worthing
02/02/2005​
900​
775
3​
Worthing
16/05/2006​
900​
775
5​
Eastbourne
01/10/2006​
775​
775
 

Forum statistics

Threads
1,082,131
Messages
5,363,337
Members
400,726
Latest member
Shahzad Taimoor

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top