如何在 T-SQL 中计算 ABCIndicator

发布于 2024-09-02 00:31:44 字数 761 浏览 8 评论 0原文

例如,我有一个销售数据表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橪书 2024-09-09 00:31:44

您可以在子查询中计算总和和运行总和。运行总和是该行以及总和较高的行的销售总额。

select
    ItemCode
,   ItemDesc
,   TotalYearlySales
,   TotalYearlySales / SalesSum as Share
,   case when RunningSum > 0.2 * SalesSum then 'A'
         when RunningSum > 0.05 * SalesSum then 'B'
         else 'C'
    end as ABCIndicator    
from (
    select
        ItemCode
    ,   ItemDesc
    ,   TotalYearlySales
    ,   (
        select sum(TotalYearlySales) 
        from @Sales t1
        where Yir = Year(getdate())
        ) as SalesSum
    ,   (
        select sum(TotalYearlySales) 
        from @Sales t2
        where Yir = Year(getdate()) 
              and TotalYearlySales <= t3.TotalYearlySales
        ) as RunningSum
    from @Sales t3
    where Yir = Year(getdate())
) sub

这打印:

ItemCode  ItemDesc      TotalYearlySales  Share  ABCIndicator
1234      Yellow Flute  3000              0,36   A
1235      Brown Violin  2000              0,24   A
1236      Silver Flute  1800              0,21   A
1236      Pink Drums    1500              0,18   B

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.

select
    ItemCode
,   ItemDesc
,   TotalYearlySales
,   TotalYearlySales / SalesSum as Share
,   case when RunningSum > 0.2 * SalesSum then 'A'
         when RunningSum > 0.05 * SalesSum then 'B'
         else 'C'
    end as ABCIndicator    
from (
    select
        ItemCode
    ,   ItemDesc
    ,   TotalYearlySales
    ,   (
        select sum(TotalYearlySales) 
        from @Sales t1
        where Yir = Year(getdate())
        ) as SalesSum
    ,   (
        select sum(TotalYearlySales) 
        from @Sales t2
        where Yir = Year(getdate()) 
              and TotalYearlySales <= t3.TotalYearlySales
        ) as RunningSum
    from @Sales t3
    where Yir = Year(getdate())
) sub

This prints:

ItemCode  ItemDesc      TotalYearlySales  Share  ABCIndicator
1234      Yellow Flute  3000              0,36   A
1235      Brown Violin  2000              0,24   A
1236      Silver Flute  1800              0,21   A
1236      Pink Drums    1500              0,18   B
几味少女 2024-09-09 00:31:44

我认为你的问题需要应用 NTILE() 函数。
这里有一些阅读内容

I think your problem will need application of NTILE() function.
There is some reading to do here

故人爱我别走 2024-09-09 00:31:44

也许像

Select *
    , ABCIndicator = case when ShareOfBusiness > 80 then 'A'
                    when ShareOfBusiness > 65 then 'B'
                    else 'C' --or whatever logic you need
                    End
From
(
    Select *
    ,ShareOfBusiness = MonthlySales/TotalYearlySales*100
    From
    (
        SELECT ItemCode
        , ItemDesc
        , Sum(Sales) TotalYearlySales
        ,Sum(case when Manth = Month(getdate()) then Sales else 0 end) MonthlySales
        --, ShareOfBusiness, ABCIndicator
        FROM Sales
        WHERE Yir = Year(getdate())
        Group By ItemCode, ItemDesc
    ) SalesSummary
) ShareOfBusiness
ORDER BY TotalYearlySales DESC

Maybe something like

Select *
    , ABCIndicator = case when ShareOfBusiness > 80 then 'A'
                    when ShareOfBusiness > 65 then 'B'
                    else 'C' --or whatever logic you need
                    End
From
(
    Select *
    ,ShareOfBusiness = MonthlySales/TotalYearlySales*100
    From
    (
        SELECT ItemCode
        , ItemDesc
        , Sum(Sales) TotalYearlySales
        ,Sum(case when Manth = Month(getdate()) then Sales else 0 end) MonthlySales
        --, ShareOfBusiness, ABCIndicator
        FROM Sales
        WHERE Yir = Year(getdate())
        Group By ItemCode, ItemDesc
    ) SalesSummary
) ShareOfBusiness
ORDER BY TotalYearlySales DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文