如何执行存储在SQL表的列中的条件

问题描述

下面,我将描述将要使用的所有表。

一个表是我们必须计算成本的基本表。

Del_Line表

enter image description here

第二个表是Outbound_Cost_Table

enter image description here

第三张表包含一列,该列具有每个国家/地区的计算城市 plant_code

计算表

enter image description here

可能会有更多的国家和城市采用不同的工厂代码和计算方法。 我想做的是编写通用查询来计算每个交货行的出站成本,因为计算表将是动态的,并且可由业务用户随时更新。

我将向您显示用于计算一个城市出境成本的示例查询

with cte as (
Select A.plant_code,B.Cost/(Sum(A.Packs)*2) cost_per_pack
from #del_line A left join Outbound_Cost B  
on A.dl_plant_code = B.Plant_code and A.city = B.City
group by A.[DL_Plant_Code],B.Cost
)

Select A.*,B.cost_per_pack*A.Packs*2 as Outbound_Handling_Cost
from #del_line A left join CTE B 
On  A.dl_plant_code = B.dl_plant_code

在以上查询中,我正在为达拉斯进行计算。

现在,我想编写一个通用查询,而不必在每个城市中进行编码。

我想要这样的东西

with cte as (
Select A.plant_code,C.Calc cost_per_pack
from #del_line A left join Outbound_Cost B  
on A.dl_plant_code = B.Plant_code and A.city = B.City left Join #Calc C on A.Market = C.Market and C.kpi = 'Outbound Handling'and C.kpi_level = 'Delivery Line'
group by A.[DL_Plant_Code],C.calc as Outbound_Handling_Cost
from #del_line A left join CTE B 
On  A.dl_plant_code = B.dl_plant_code left Join #Calc C on A.Market = C.Market and C.Handling = 'Outbound Handling'  and C.[KPI Level] = 'Delivery Line Drilldown'


由于基表del_line非常大,请避免使用循环。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)