如何在 T-SQL 中计算 ABCIndicator
例如,我有一个销售数据表:
SELECT ItemCode, ItemDesc, TotalYearlySales, ShareOfBusiness, ABCIndicator
FROM Sales
WHERE Yir = Year(getdate())
AND Manth = Month(getdate())
ORDER BY TotalYearlySales DESC
ShareOfBusiness 计算为项目的 (TotalYearlySales/SUM(TotalYearlySales))*100
ABCIndicator 是 A,表示总和为 80% 的项目列表。 SUM(业务份额)。 B 代表接下来的 15%,C 代表最后 5%。
如何获取每个项目的 ABCIndicator?
样本数据:
ItemCode ItemDesc TotalYearlySales ShareOfBusiness ABCIndicator
1234 Yellow Flute 3000 .36 A
1235 Brown Violin 2000 .24 A
1236 Silver Flute 1800 .21 A
1236 Pink Drums 1500 .18 B
I have a table of sales data for example:
SELECT ItemCode, ItemDesc, TotalYearlySales, ShareOfBusiness, ABCIndicator
FROM Sales
WHERE Yir = Year(getdate())
AND Manth = Month(getdate())
ORDER BY TotalYearlySales DESC
The ShareOfBusiness is computed as the Item's (TotalYearlySales/SUM(TotalYearlySales))*100
The ABCIndicator is A for the list of items whose sum is 80% of the SUM(ShareOfBusiness). B for the next 15% and C for the last 5%.
How can I get the ABCIndicator for each item?
Sample Data:
ItemCode ItemDesc TotalYearlySales ShareOfBusiness ABCIndicator
1234 Yellow Flute 3000 .36 A
1235 Brown Violin 2000 .24 A
1236 Silver Flute 1800 .21 A
1236 Pink Drums 1500 .18 B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在子查询中计算总和和运行总和。运行总和是该行以及总和较高的行的销售总额。
这打印:
You could compute the sum and the running sum in a subquery. The running sum is the total amount of sales for that row and rows with a higher sum.
This prints:
我认为你的问题需要应用 NTILE() 函数。
这里有一些阅读内容
I think your problem will need application of NTILE() function.
There is some reading to do here
也许像
Maybe something like