【问题标题】:How to Compute for ABCIndicator in T-SQL如何在 T-SQL 中计算 ABCIndicator
【发布时间】:2011-02-20 09:41:14
【问题描述】:

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

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

对于总和为 SUM(ShareOfBusiness) 的 80% 的项目列表,ABCIndicator 是 A。 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

【问题讨论】:

  • 你能提供一些示例数据/输出吗?

标签: sql tsql select subquery


【解决方案1】:

我认为您的问题需要应用 NTILE() 函数。 有一些阅读要做here

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      【解决方案3】:

      也许像

      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
      

      【讨论】:

        猜你喜欢
        • 2012-01-22
        • 1970-01-01
        • 2012-05-08
        • 1970-01-01
        • 2011-10-16
        • 2010-09-08
        • 2013-02-22
        • 2018-09-02
        相关资源
        最近更新 更多