根据范围将总和分为几类

时间:2019-12-12 15:18:22

标签: sql sql-server

我正在尝试使用MSSQL将销售总额划分为几类。

例如,十月的总销售是557.361。我需要将总和分为三个类别,根据每个类别计算奖金价格,而其余金额则溢出到下一个类别。

使用MSSQL是否可能?

请参见以下示例:

IF OBJECT_ID('tempdb..#sales') IS NOT NULL
    DROP TABLE #sales;

CREATE TABLE #sales
(
    year_month VARCHAR(10),
    total_sales INT
);


INSERT INTO #sales
(
    year_month,
    total_sales
)
VALUES
('2019-10-01', 557361),
('2019-11-01', 621801);

这将是理想的结果,请参见下图。

enter image description here

2 个答案:

答案 0 :(得分:2)

在表类别中,我用范围(最大值-最小值)替换了范围,该范围在查询中比其中带有iphen的字符串更有用。 我添加了lso Catergory订单来确定溢出顺序,其名为CategoyOrder 试试这个,让我们知道:

/ *************数据源********************** /

IF OBJECT_ID('tempdb..#sales') IS NOT NULL
    DROP TABLE #sales;

CREATE TABLE #sales
(
    year_month VARCHAR(10),
    total_sales INT
);


INSERT INTO #sales
(
    year_month,
    total_sales
)
VALUES
('2019-10-01', 557361),
('2019-11-01', 621801);

select * from #sales

IF OBJECT_ID('tempdb..#categories') IS NOT NULL
    DROP TABLE #categories;

CREATE TABLE #categories
(
    CategoryOrder int ,
    Category VARCHAR(10),
    price decimal(5,2),
    discount decimal(5,2),
    RangeSpread int ,
    CumulRangeSpread int 
);

INSERT INTO #categories
(
    CategoryOrder,
    Category,
    price,
    discount,
    RangeSpread,
    CumulRangeSpread  
)
VALUES
(1,'Small', 15, 0, 50000, 50000),
(2,'Medium', 13, 15, 350000, 400000),
(3,'Big', 11, 25, 400000, 800000);

select * from #categories

/ *************结束***************** /

/ *************查询QU ****************** /

;with cte as
(select c1.CategoryOrder, c1.Category, c1.price, c1.discount, c1.RangeSpread, isnull(c2.CumulRangeSpread, 0) as CumulRangeSpread from #categories c1 left join #categories c2 on c1.CategoryOrder -1 = c2.CategoryOrder) 

select c.CategoryOrder, c.Category, c.price, c.discount, total_sales, 
case when c.RangeSpread < total_sales - CumulRangeSpread  then c.RangeSpread else total_sales - CumulRangeSpread end as [CountIncategory], 
c.price*(case when c.RangeSpread < total_sales - CumulRangeSpread  then c.RangeSpread else total_sales - CumulRangeSpread end) as [Count*Price]
from cte c cross join #sales s order by total_sales, 2 desc

答案 1 :(得分:1)

我认为该解决方案不需要花哨的任何事情,只需要一些条件逻辑和算术即可。这是执行计算的脚本。为了方便起见,您可以将其打包成一个函数。该函数可以接受@total_sales_base作为参数。

declare @total_sales_base int = 557361
declare @total_sales decimal(10,3) = @total_sales_base / 1000.0

drop table if exists #tmp
create table #tmp (Category varchar(10), Total decimal(10,3))

declare @sub decimal(10,3) = 0.0
declare @bonus decimal(10,3) = 0.0

set @sub = @total_sales - 50.0

if (@sub > 0.0)
begin
  set @bonus = 750.0
  set @total_sales = @total_sales - 50.0
end
else
begin
  set @bonus = @total_sales * 15
  set @total_sales = 0.0
end

insert into #tmp select 'Small', @bonus

set @sub = @total_sales - 350.0

if (@sub > 0.0)
begin
  set @bonus = 4550.0
  set @total_sales = @total_sales - 350.0
end
else
begin
  set @bonus = @total_sales * 13
  set @total_sales = 0.0
end

insert into #tmp select 'Medium', @bonus

set @bonus = @total_sales * 11

insert into #tmp select 'Big', @bonus

select * from #tmp

输出:

+----------+----------+
| Category | Total    |
+----------+----------+
| Small    |  750.000 |
| Medium   | 4550.000 |
| Big      | 1730.971 |
+----------+----------+