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,585
Messages
5,366,466
Members
400,892
Latest member
lamarh755

Some videos you may like

This Week's Hot Topics

Top