Selecting records by a calculation

chris-evans

Board Regular
Joined
Feb 19, 2002
Messages
73
Hoping someone can help me.

I have a table of a couple of thousand records, it lists customer name in one field and revenue in the next. I want to run a query where it give me all the customer names that comprise 80% of the total revenue for the table.

Is this possible and if so how can I do this!?

Many thanks

Chris
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Chris

I got this to work using 3 separate queries, as follows:

Query 1
Code:
SELECT tblSales.Customer, Sum(tblSales.Sales) AS SumOfSales, (SELECT Sum([Sales]) FROM tblSALES) AS GTOTAL
FROM tblSales
GROUP BY tblSales.Customer
ORDER BY Sum(tblSales.Sales) DESC;
This is the SQL for the first query (saved as qrySales1) - you can get into the SQL view by selecting View > SQL View when in the query design screen. This query adds up the total sales by customer and sorts them in descending order and also provides a grand total for sales in the table.

Query 2
Code:
SELECT qryAlias.Customer, qryAlias.SumOfSales, qryAlias.GTOTAL, (SELECT Sum([SumOfSales]) FROM qrySales1 WHERE SumofSales >= qryAlias.SumofSales) AS RunTOTAL, [RunTOTAL]/[GTOTAL] AS MyPercent
FROM qrySales1 AS qryAlias;
This query calculates a running total and a percentage running total. Save this query as qrySales2.

Query 3
Code:
SELECT qrySales2.Customer, qrySales2.SumOfSales, qrySales2.MyPercent
FROM qrySales2
WHERE (((qrySales2.MyPercent)<=(SELECT Min(MyPercent) FROM qrySales2 WHERE qrySales2.MyPercent >= 0.8)));
This query only displays the largest customers who provided at least 80% of the total sales value. I seemed to get an error when I tried to sort this last query (Giacomo are you watching? Any clues on that one?) so beware of that.

Use your actual actual table name in place of 'tblSales' are your actual field names in place of 'Customer' and 'Sales'.

HTH, Andrew
 
Upvote 0
(Giacomo are you watching? Any clues on that one?)
:LOL:

Andrew, instead of sorting the field by name try using order by 3 DESC and see if that works.

Giacomo
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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