【问题标题】:Need to calculate cost based on Weight percentage需要根据重量百分比计算成本
【发布时间】:2021-11-15 19:04:01
【问题描述】:

我需要根据重量和成本的计算创建一份报告。我做过的其他事情,但缺少的是如何计算成本,我不知道如何实现这一点。 有人可以建议如何实现此输出。

这是测试数据。

Create table #temp
(
    ID int,
    StopNumber int,
    [Weight] int,
    Cost decimal (18,2),
    Category nvarchar(max)
)

Insert into #temp values (1,1,5719,3099,'Linehaul')
Insert into #temp values (1,2,2627,393.82,'Fuel')
Insert into #temp values (1,3,3096,215,'Accessorial')
Insert into #temp values (2,1,6000,4500,'Linehaul')
Insert into #temp values (2,2,5000,383.82,'Fuel')
Insert into #temp values (2,3,4000,315,'Accessorial')

select * from #temp

ID  StopNumber  Weight  Cost    Category 
1       1       5719    3099.00 Linehaul 
1       2       2627    393.82  Fuel 
1       3       3096    215.00  Accessorial 
2       1       6000    4500.00 Linehaul 
2       2       5000    383.82  Fuel 
2       3       4000    315.00  Accessorial 

预期输出

ID  StopNumber  Weight  Cost    Category    LineHaul    Fuel    Accessorial  
1      1        5719    3099.00 Linehaul    1,548.96    196.84  107.46 
1      2        2627    393.82  Fuel        711.51      90.42   49.36 
1      3        3096    215.00  Accessorial 838.53      106.56  58.18 
2      1        6000    4500.00 Linehaul    1,800.00    153.53  126 
2      2        5000    383.82  Fuel        1,500.00    128     105 
2      3        4000    315.00  Accessorial 1,200.00    102.35  84  

需要根据重量百分比来计算 Linehaul、Fuel 和 Accessorial 成本。

例如:ID 1 的权重总和 = 11442 ID 2的权重之和=15000

现在 5719/11442 = 50%
2727/11442 = 23%
3096/11442 = 27%

ID 1 = 3099 的线性总成本
ID 1 的燃料总成本 = 393.82
ID 1 的辅助总成本 = 215

所以 Linehaul 成本将根据百分比计算在 3 个权重中分配

3099 * 50 % = 1548.96
3099 * 23 % = 711.51
3099 * 27 % = 838.53

对于燃料和配件成本以及不同的 ID,将进行相同的计算。

【问题讨论】:

  • SQL Server 2008 还是 2012?请仅标记您正在使用的 RDBMS 版本。
  • 您可能正在寻找一个窗口函数来计算您的总数以计算比例。

标签: sql sql-server tsql sql-server-2008 sql-server-2012


【解决方案1】:

您可以使用窗口函数来计算每个 id 的总数。剩下的只是算术。如果我正确地遵循了您的逻辑:

select t.*,
       (weight / id_linehaul) * (cost) as linehaul,
       (weight / id_fuel) * (cost) as fuel,
       (weight / id_accessorial) * (cost) as accessorial
from (select t.*,
             sum(weight) over (partition by id) as id_weight,
             sum(case when category = 'Linehaul' then cost end) over (partition by id) as id_linehaul,
             sum(case when category = 'Fuel' then cost end) over (partition by id) as id_fuel,
             sum(case when category = 'Accessorial' then cost end) over (partition by id) as id_accessorial
      from t
     ) t

【讨论】:

  • 感谢您的快速响应,但输出不正确戈登。 Linehaul、燃料和辅助成本未按预期计算
【解决方案2】:

将窗口函数与sum() 聚合一起使用。

按 ID 计算总重量

sum([Weight]) over (partition by ID)`

同样,您可以使用 CASE 表达式和窗口函数来查找 ID 类别的成本,例如 Linehaul

sum(case when Category = 'Linehaul' then [Cost] end) over (partition by ID)

select  *,
        Linehaul    = [Weight] * 1.0 / sum([Weight]) over (partition by ID) 
                    * sum(case when Category = 'Linehaul' then [Cost] end) over (partition by ID),
        Fuel        = [Weight] * 1.0 / sum([Weight]) over (partition by ID) 
                    * sum(case when Category = 'Fuel' then [Cost] end) over (partition by ID),
        Accessorial = [Weight] * 1.0 / sum([Weight]) over (partition by ID) 
                    * sum(case when Category = 'Accessorial' then [Cost] end) over (partition by ID)
from    #temp t
order by ID, StopNumber

dbfiddle demo

【讨论】:

  • 太棒了,这就是我要找的。非常感谢!!
猜你喜欢
  • 2014-06-24
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 2023-01-16
  • 2013-10-17
相关资源
最近更新 更多