如何从外部引用(SQL)获得更高或更低的值

时间:2015-01-13 19:39:21

标签: sql sql-server

我有两张表,一张是材料数据,以及他们目前拥有的成本大小。然后我有另一张表格,其中包含相同材料的购买比例。然后,我需要从原始表中获取与我的成本计算批量相同或更小的购买规模,并在其上方获取购买规模。这对我来说可以作为我的选择句子中的相关子查询:

Select 
mbew.Matnr as Material
,mbew.bwkey as Plant
,mbew.STPRS/mbew.peinh as Standard_price
,mbew.verpr/mbew.peinh as Average_price
,keko.Costing_lot_size
,Keko.Costing_date
,(select max(info.scale_quantity_konm) as Scale_quantity from dwh.inforecords info where mbew.matnr = info.material and keko.costing_lot_size >= info.scale_quantity_konm) as Current_Costing_Lot_Size
,(select min(info.scale_quantity_konm) as Scale_quantity from dwh.inforecords info where mbew.matnr = info.material and keko.costing_lot_size < info.scale_quantity_konm) as Next_Costing_Lot_Size
,inforecords_larger_price.amount_dkk_konm as Next_Costing_Lot_Size_Price


from 
mbew mbew

left join

(Select kekoa.Material, kekoa.Plant, kekoa.Costing_date, kekoa.Costing_lot_size
from sap.keko Kekoa

inner join 
(select Material, Plant, max(costing_date) as Lates_costing_date from sap.keko
group by material, plant) kekob

on kekoa.costing_date = kekob.Lates_costing_date
and kekoa.Material = kekob.material
and kekoa.plant  = kekob.plant) as keko

on 
mbew.matnr = keko.material
and mbew.bwkey = keko.Plant

非常低效。它可以以某种方式变得更聪明/优化吗?

提前致谢

0 个答案:

没有答案