[英]How can I select data from two tables using an inner join and insert directly into a third table?
[英]how can I take two tables and merge them into a third without a “temp” table?
我有幾個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;
謝謝。
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);
您可以將子查詢用於USING。 我只是用您選擇的替換了tmp表(用於插入tmp)
實際上,您不需要合並
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.