简体   繁体   English

如何获取两个表并将它们合并为没有“临时”表的第三个表?

[英]how can I take two tables and merge them into a third without a “temp” table?

I have a few oracle strings that is doing a merge and an inset and then merge. 我有几个oracle字符串正在执行合并和插入,然后合并。 I got it to work this way but due to company restrictions, adding tables takes at least two weeks. 我让它以这种方式工作,但是由于公司的限制,添加表至少需要两周时间。 (I made the site_item_temp table just to get this command set to work.) The oracle Server does not have this table and we cannot sit and wait while the DBAs take their time adding the table, or any table that is with the proper privileges and such. (我制作了site_item_temp表只是为了将此命令设置为起作用。)oracle服务器没有该表,我们无法坐等DBA花时间添加表或具有适当特权的任何表,并且这样。

I need this to run without hitting the site_item_temp table, or any other table except those listed. 我需要运行它而无需点击site_item_temp表或除列出的表以外的任何其他表。 due to DBA's restrictions/time limits. 由于DBA的限制/时间限制。 I'm scratching my head with this. 我为此抓挠头。 it is probably very simple but I'm just drawing a blank. 这可能非常简单,但我只是在画一个空白。

Here are the scripts I run. 这是我运行的脚本。

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;

thank you. 谢谢。

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);

You can use subqueries for USING. 您可以将子查询用于USING。 I just replaced tmp table with you select (used for insert into tmp) 我只是用您选择的替换了tmp表(用于插入tmp)

Actually, you don't need a 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用内部联接从两个表中 select 数据并直接插入第三个表? - How can I select data from two tables using an inner join and insert directly into a third table? 如何在不命名所有列的情况下合并两个表? - How do I merge two tables without naming all columns? 如何从两个不同的表中获取数据,如何将其链接到第三个表中的数据,以及如何在单个报表中获取所有数据? - How can I get data from two different tables, link it to data from a third table, and get everything on a single report? 如何在oracle中使用ROWID合并两个表? - How can I merge two tables using ROWID in oracle? 通过映射第三个表查询联接两个表,而无需从Oracle中的第三个表返回所有记录 - Query to join two tables by mapping third table without returning all records from third table in Oracle 如何查询一张表,并将结果作为两张表返回 - How can i query one table, and return the result as two tables 如何在Oracle中将两个或多个未知表合并为一个表 - How to merge two or more unknown tables into one table in Oracle 使用Oracle MERGE INTO将两个表中的数据合并到第三个表中 - Merging data from two tables into a third using Oracle MERGE INTO 如何在存储过程中创建临时表和 select? - How can I create temp table and select it in a stored procedure? 如何比较两个不同表的两个不同行并更新第三个表? - How to compare two different rows of two different tables and update a third table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM