I need to select all itemcodes upto 95% of the running total column for each group
Main Topics
Browse All TopicsI need to add additional column 'total' to the result set, where it has running total
and select only itemcodes that accounts to total of 95% of cumulative catqty. Any help is greatly appreciated.
Query is as follows:
SELECT v1.category, v1.itemcode, v1.itmqty, v2.catqty, v1.itmqty/v2.catqty*100,
[*Running Total column of v1.itmqty/v2.catqty*100 <--- Code to add this functionality]
FROM [SELECT category,itemcode, sum(qty) as itmqty
FROM itemtable
WHERE nz(qty,0) > 0
GROUP BY category,itemcode
order by 1, 3 desc]. AS v1
INNER JOIN
[SELECT category, sum(qty) as catqty
from item_table
where nz(qty,0) > 0
group by category]. AS v2
ON v1.category = v2.category
WHERE v1.itmqty/v2.catqty <= 95.00
order by 1,5 desc
Thank You
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is an example
Data in the table looks something like this:
Category Itemcode OrdQty
A A100 40
A A100 1
A A110 30
A A111 15
A A123 10
A A999 3
A Z121 1
Output should look like this
totalitmqty --> Total of Ordqty of each unique item
catqty == Total qty of all the items in each category
%ofeachitemo= --> % of each item ordered i.e. totalitmqty/catqty*100
Category Itemcode totalitmQty Catqty %ofeachitem RunningTotal
A A100 41 100 41% 41%
A A110 30 100 30% 71%
A A111 15 100 15% 86%
A A123 10 100 10% 96%
A A999 3 100 3% 99%
A Z121 1 100 1% 100%
So in the final output, I want to pickup only items that accounts for <= 95% of the totalqty ordered for each entity. In this case it will be only 3 these rows and the items are A100,A110 & A111. In my example, I have given illustration of only entity A. I will have more than one entity.
A A100 41 100 41% 41%
A A110 30 100 30% 71%
A A111 15 100 15% 86%
Try this, I works for me.
SELECT a.Category, a.ItemCode, Sum(a.OrdQty) AS ItemQty, DSum("OrdQty","myTbl","Cat
(SELECT Format(Sum(b.OrdQty/DSum("
FROM myTbl a WHERE (SELECT Sum(b.OrdQty/DSum("OrdQty"
GROUP BY a.Category, a.ItemCode ;
Business Accounts
Answer for Membership
by: GRayLPosted on 2009-04-30 at 14:32:24ID: 24274641
How many records do you want to see?