Creating a query to filter customers

jakeman

Active Member
Joined
Apr 29, 2008
Messages
325
Office Version
  1. 365
Platform
  1. Windows
Hi All - I have a working query that allows me to filter out customers who have less than $60,000 in sales. However, I needed to add a Period field to the Query to see sales in each of the last four years. The only thing now is that using a simple filter in my query after adding the Period field, doesn't work for me anymore.

For example, CUSTXYZ has sales like this:
customer
period
sales
CUSTXYZ
2014
$62,000
CUSTXYZ
2015
$86,000
CUSTXYZ
2016
$92,000
CUSTXYZ
2017
$72,000
CUSTXYZ
2018
$52,000

<tbody>
</tbody>

When I apply my Criteria to the Sales field (>=60000), sales for period 2018 gets eliminated. Is there a way that allows me to filter out records in my query where the combined sales for a customer over a 4 year period are less than $60,000?
 
I created a small test database, with two tables: tblSales and tblCustomer. tblSales was as your example, with a second company, COMPANYSTU, with sales over the period < 60,000. First, I rewrote your query to get a similar join.

Code:
SELECT [tblSales].[Posting Year],
 [tblSales].[Cust Nbr],
 [tblCustomer].[Cust Name] AS [Cust Name],
 [tblSales].Sales
FROM [tblSales] INNER JOIN [tblCustomer]
 ON [tblSales].[Cust Nbr] = [tblCustomer].No_;

Then, I used those results to calculate the sum over the period, then grouped by Cust Nbr.

Code:
SELECT  SUM(A.Sales), A.[Cust Nbr] FROM (SELECT [tblSales].[Posting Year],
 [tblSales].[Cust Nbr],
 [tblCustomer].[Cust Name] AS [Cust Name],
 [tblSales].Sales
FROM [tblSales] INNER JOIN [tblCustomer]
 ON [tblSales].[Cust Nbr] = [tblCustomer].No_) as A
GROUP BY A.[Cust Nbr]
HAVING SUM(A.Sales) >60000 
;

I then saved that the above query as Query2, and created another, Query 3, to join back to tblCustomer to get the names. I used the Design View for this step (Note my earlier suggestion, build step by step with Design View, then copy and paste into the criteria fields). The calculated sql was:

Code:
SELECT tblCustomer.[Cust Name], Query2.Expr1000
FROM Query2 INNER JOIN tblCustomer ON Query2.[Cust Nbr] = tblCustomer.No_;
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Forum statistics

Threads
1,213,487
Messages
6,113,943
Members
448,534
Latest member
benefuexx

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