SQL Access Query

PBE

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

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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
 
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top