需要根据重量百分比计算成本

Need to calculate cost based on Weight percentage

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

这里是测试数据

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  

需要根据重量百分比计算长途运输、燃料和配件成本。

例如: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,燃料和辅助成本也将进行相同的计算。

您可以使用window函数来计算每个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

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

按 ID 计算总重量

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

同样,您可以使用 CASE 表达式和 window 函数来查找 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