如何在没有" temp"的情况下将两个表合并为第三个表。表?

时间:2014-10-21 18:47:46

标签: oracle merge

我有一些oracle字符串正在进行合并和插入然后合并。我让它以这种方式工作,但由于公司的限制,添加表至少需要两周时间。 (我创建了site_item_temp表只是为了让这个命令设置工作。)oracle服务器没有这个表,我们不能等待DBA花时间添加表,或任何具有适当权限的表,这样

我需要在没有访问site_item_temp表或除列出的表之外的任何其他表的情况下运行它。由于DBA的限制/时间限制。我为此挠挠脑袋。它可能很简单,但我只是画了一个空白。

以下是我运行的脚本。

MERGE INTO SITE_ITEM_MASTER D 
 USING ITEM_MST S 
    ON (D.SKU = S.INVEN_ID 
    and d.loc_id = s.loc_id)

WHEN NOT MATCHED THEN 
INSERT (D.SKU, D.CASES_PER_PALLET, D.LOC_ID) 
VALUES (S.INVEN_ID, S.CASE_PER_PAL_QTY, S.LOC_ID);



  DELETE FROM SITE_ITEM_TEMP;



  INSERT INTO SITE_ITEM_TEMP ( SKU, CASES_PER_PALLET, LOC_ID )
 SELECT ITEM_MST.INVEN_ID, AVG(ITEM_MST.CASE_PER_PAL_QTY) AS AVGOFCASE_PER_PAL_QTY, 
   ICAM_LOCATIONS.LOCATION_ID
   FROM ITEM_MST, ICAM_LOCATIONS
  GROUP BY ITEM_MST.INVEN_ID, ICAM_LOCATIONS.Location_ID;



  MERGE INTO SITE_ITEM_MASTER D 
 USING SITE_ITEM_TEMP S
    ON (D.SKU = S.SKU 
    AND D.LOC_ID = S.LOC_ID)

  WHEN NOT MATCHED THEN 
INSERT (D.SKU, D.CASES_PER_PALLET, D.LOC_ID) 
VALUES (S.SKU, S.CASES_PER_PALLET, S.LOC_ID);



  DELETE FROM SITE_ITEM_TEMP;
谢谢。

1 个答案:

答案 0 :(得分:1)

merge into site_item_master d 
 using item_mst s 
    on (d.sku = s.inven_id     
    and d.loc_id = s.loc_id)
when not matched then 
    insert (d.sku, d.cases_per_pallet, d.loc_id) 
    values (s.inven_id, s.case_per_pal_qty, s.loc_id);    

merge into site_item_master d 
 using (
       select item_mst.inven_id sku, avg(item_mst.case_per_pal_qty) as cases_per_pallet, icam_locations.location_id loc_id                
         FROM ITEM_MST, ICAM_LOCATIONS
        group by item_mst.inven_id, icam_locations.location_id
       ) s 
    on (d.sku = s.sku and d.loc_id = s.loc_id)
  when not matched then   
    insert (d.sku, d.cases_per_pallet, d.loc_id) 
    values (s.sku, s.cases_per_pallet, s.loc_id);

您可以使用子查询进行使用。我刚刚用你的select替换了tmp表(用于插入到tmp中)

实际上,您不需要MERGE

insert into site_item_master (sku, cases_per_pallet, loc_id) 
select inven_id, case_per_pal_qty, loc_id from(
  select inven_id, case_per_pal_qty, loc_id 
    from item_mst
  union all
  select item_mst.inven_id sku, avg(item_mst.case_per_pal_qty), icam_locations.location_id
   from item_mst, icam_locations 
  group by item_mst.inven_id, icam_locations.location_id
) s
where not exists (select 1 from site_item_master d where d.sku = s.inven_id and d.loc_id = s.loc_id);