473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sql statement to select customers contributing top 60% of sales

I have an Access 2000 database which lists customers by name, and how
much (in dollars) they have purchased of various products. How do I
write a SQL statement to select customers who make up the top 60% of
total sales dollars? I need to have a list of customers returned.

The list of customers that get returned will be used to select other
data from the same database. (I mention this in case there are multiple

ways of getting the customer list).
Thank you for any advice.

Oct 18 '06 #1
2 2096
Try something of the format:

SELECT cs.cus_id, cs.cus_name, sum(sales) as sales
FROM cus_sales cs
INNER JOIN ( SELECT sum(sales) tot_sales FROM cus_sales ) ts
ON cs.sales >= ts.tot_sales * .6

ka**********@gmail.com wrote:
I have an Access 2000 database which lists customers by name, and how
much (in dollars) they have purchased of various products. How do I
write a SQL statement to select customers who make up the top 60% of
total sales dollars? I need to have a list of customers returned.

The list of customers that get returned will be used to select other
data from the same database. (I mention this in case there are multiple

ways of getting the customer list).
Thank you for any advice.
Oct 18 '06 #2
Bill wrote:
ka**********@gmail.com wrote:
>I have an Access 2000 database which lists customers by name, and how
much (in dollars) they have purchased of various products. How do I
write a SQL statement to select customers who make up the top 60% of
total sales dollars? I need to have a list of customers returned.

The list of customers that get returned will be used to select other
data from the same database. (I mention this in case there are multiple
ways of getting the customer list).
Try something of the format:

SELECT cs.cus_id, cs.cus_name, sum(sales) as sales
FROM cus_sales cs
INNER JOIN ( SELECT sum(sales) tot_sales FROM cus_sales ) ts
ON cs.sales >= ts.tot_sales * .6
[top-posting fixed]

1) "GROUP BY cs.cus_id, cs.cus_name" is missing
2) This would only return data if a single customer accounts for
60% or more of total sales
3) "sum(sales) as sales" is probably a bad idea
4) cus_name should be in customers, not cus_sales

I think the following would work. Perhaps someone can collapse this
into a single query, suitable for stuffing into a view.

create table #t1 (
cus_id int,
tot_sales_cus decimal(15,2),
cus_sales_rank int,
tot_sales_running decimal(15,2)
)

insert into #t1 (cus_id, tot_sales_cus)
select cus_id, sum(sales)
from cus_sales
group by cus_id

update #t1 as x
set cus_sales_rank = 1 + (
select count(*)
from #t1 as y
where y.tot_sales_cus x.tot_sales_cus
)

update #t1 as x
set tot_sales_running = (
select sum(tot_sales_cus)
from #t1 as y
where y.cus_sales_rank <= x.cus_sales_rank)

declare @tot_sales_company decimal(15,2)

select @tot_sales_company = (select sum(sales) from cus_sales)

select x.cus_id, c.cus_name, x.tot_sales_cus
from tot_sales_running as x
join cus_sales as c
where 100 * x.tot_sales_cus / @tot_sales_company >= 60
Oct 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sam G | last post by:
Hi all, I have a statement that reads as follows: select - gl as difference from (SELECT SUM(RPAAP / 100) AS FROM F03B11) Q1 join (SELECT SUM(GBAPYC + GBAN01 + GBAN02 +...
6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
9
by: | last post by:
Is it possible to construct a CDO.To statement based on the value of an incoming form drop down list which contains any word such as "ChangeStatus:" How would I construct the IF statement to...
5
by: Fred | last post by:
Hi all, I have done a lot of experimentations using several "or" predicates within an sql select statements on mysql. My only conclusion so far is that whatever syntax or order I use it is...
0
by: Suler Abou | last post by:
Hi, I'm having a problem with an SQL statement, I have a statement that goes like this: "INSERT INTO table VALUES('TransID','CID',etc...);" it basically adds new data to a table. When the...
9
imrosie
by: imrosie | last post by:
Hello Experts, It's going to take one to figure this out. My Order form is supported by a query of two tables (Customers and Orders). There is a subform within the Form for entering in the new...
2
by: gggram2000 | last post by:
Hi! I have a SQLDataAdapter and I want to retrieve data from my database in SQL Server 2005 . Supposing I have two tables named Guest, Sales and one of the columns is GuestFirstName. I want to...
4
by: AKdlm | last post by:
I am using Access 97 (I know) and am creating a lost and found database for materials in our company. I have it all set up so an employee can enter a sales order and pick a specific line/multiple...
5
by: DBlearner | last post by:
Dudes, you should check this out! I just downloaded a MS Access database titled "Sales Pipeline" and it's a great way of learning codes to do charts on the fly. I'm reading the codes and checking...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.