Dear Experts,
I have a list of customers with their respective sales. there are multiple sales for few customers.
The scripted form of table data is as follows:-
CREATE TABLE mytable (
Cust VARCHAR(1) NOT NULL PRIMARY KEY
,Sales INT NOT NULL
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'A'
,100
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'B'
,300
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'C'
,500
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'D'
,1000
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'E'
,100
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'F'
,50
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'G'
,300
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'I'
,400
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'G'
,500
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'A'
,600
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'E'
,700
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'G'
,800
);
INSERT INTO mytable (
Cust
,Sales
)
VALUES (
'I'
,800
);
I need to have list of customers who are contributing to 80% of the total sales. In above data the total sales is Rs. 6150. 80% of 6150 is Rs. 4920/-. I need only those records (after totalling sales amount customerwise and sorting the records on salesamount in descending order) of the customers who are contributing to 80% of total sales turnover. That means the records in which only totalling of sales is <= 4920/-
Following is the output needed as per above requriement.
Output
+------+-------+----------------------------------+
| Cust | Sales | 80% Sales Contributing Customers |
+------+-------+----------------------------------+
| G | 1600 | 1600 |
| I | 1200 | 2800 |
| D | 1000 | 3800 |
| E | 800 | 4600 |
+------+-------+----------------------------------+
Thanks for the help in advance