select UPPER(unitofmeasurePlural) unitofmeasure from tblManufacturers where mfr_id = with priceHist as ( select distinct cast( pd.custom_3 + '/01/' + pd.custom_4 as date) as pricePeriod from tbl_IMPORT_ProductData pd with (nolock) where pd.ownerType = and pd.ownerId = and pd.eai_batchId = and ( nullif(pd.custom_4,'') is not null and nullif(pd.custom_3,'') is not null ) ) select pricePeriod from priceHist order by pricePeriod asc with priceUpload as ( select pd.sku_skuid, pd.sku_mfrid, pd.sku_sku, pd.sku_skudesc, pd.sku_unitPrice, pd.custom_2 as sku_unitPrice_CAD, pd.custom_1 as sku_unitWeight, pd.custom_3 as sku_month, pd.custom_4 as sku_year, cast( pd.custom_3 + '/01/' + pd.custom_4 as date) as pricePeriod from tbl_IMPORT_ProductData pd with (nolock) where pd.ownerType = and pd.ownerId = and pd.eai_batchId = and pd.sku_skuid is not null and ( nullif(pd.custom_4,'') is not null and nullif(pd.custom_3,'') is not null ) ) select p.sku_skuid, p.sku_mfrid, p.sku_sku, coalesce( nullif(p.sku_skuDesc,'') ,s.skuDesc ) sku_skuDesc, p.sku_unitPrice, p.sku_unitPrice_CAD, p.sku_unitWeight, p.sku_month, p.sku_year, cast( p.sku_month + '/01/' + p.sku_year as date) as pricePeriod, dw.catalogId as dw_skuId, dw.unitPrice_USD, dw.unitPrice_CAD, dw.salesPeriod, dw.uom from priceUpload p left outer join tbl_DW_PriceHistory_#attributes.ownerType##attributes.ownerid# dw with (nolock) on dw.catalogType = 'SKU' and dw.catalogId = p.sku_skuid and pricePeriod = salesPeriod left outer join tbl_PRD_Skus s with (nolock) on dw.catalogType = 'SKU' and dw.ownerid = s.ownerId and dw.ownerType = s.fsl_TableCode and dw.catalogId = s.skuId order by p.sku_sku insert into tbl_DW_PriceHistory_#attributes.ownerType##attributes.ownerid#(ownerType, ownerid, salesPeriod, catalogType, catalogId, UOM, unitPrice_USD, unitPrice_CAD, datadate, fromPeriod) values ( , , , 'SKU', , , , , getDate(), ) update tbl_DW_PriceHistory_#attributes.ownerType##attributes.ownerid# set UOM = , unitPrice_USD = , unitPrice_CAD = , datadate = getDate(), fromPeriod = where salesPeriod = and catalogType = 'SKU' and catalogId =