背景:源数据中物料不会每个月都存对应的价格,仅当物料价格发生变动的时候才会存。因此当前月份无价格的时候,需要将上个月的价格作为当月的价格
SELECT T2.WERKS,
T2.KALNR,
${PERIOD} PERIOD,
T2.CURTP,
T2.MATNR,
T2.PEINH,
T2.VPRSV,
T2.STPRS,
T2.PVPRS,
T2.STDPRICE,
T2.CYCPRICE
FROM (SELECT T1.WERKS,
T1.KALNR,
T1.PERIOD,
T1.CURTP,
T1.MATNR,
T1.PEINH,
T1.VPRSV,
T1.STPRS,
T1.PVPRS,
T1.STDPRICE,
T1.CYCPRICE,
ROW_NUMBER() OVER ( PARTITION BY T1.KALNR ORDER BY T1.KALNR,T1.PERIOD DESC ) ROWNM --按照日期降序,分组取第一条
FROM ODS_SAP_MATERIAL_PRICE T1
WHERE T1.PVPRS <> 0
AND T1.PERIOD <= ${PERIOD}) T2
WHERE T2.ROWNM = 1