我是PL / SQL的新手。我在初始表中有数据,名为'FLEX_PANEL_INSPECTIONS',我试图在第二个表中汇总,使用PL / SQL过程命名为'PANEL_STATUS_2'。但是,由于数据的性质,我必须编写一个case语句才能正确汇总FLEX_PANEL_INSPECTIONS中的数据。因此我创建了一个第三个中间表来桥接这两个(名为'PANEL_STATUS_1'),因为case语句不允许group by子句中的列专门对数据进行排序(据我所知 - 我得到一个错误我试着这样做)。我不想将数据存储在中间表中 - 有什么方法可以使它成为临时的(即仅在程序运行时存在,以便不保留来自'PANEL_STATUS_1'的数据);在过程中创建一个视图,或者完全不需要中间表?
对我的错误/对PL / SQL的误解的任何帮助或批评将不胜感激。这是我写的代码:
create or replace procedure PANEL_STATUS_PROCEDURE (panel_lot_id in number) as
begin
--Populate intermediate table with information about the status of the panels.
insert into PANEL_STATUS_1 (FLEX_LOT_ID, FLEX_PANEL_DMX, FLEX_PANEL_STATUS)
select FLEX_LOT_ID, FLEX_PANEL_DMX,
--Sum the status values of the 4 panel inspections. A panel passes if and only if this sum = 4.
case sum (FLEX_PANEL_STATUS)
when 4 then 1
else 0
end as new_panel_status
from FLEX_PANEL_INSPECTIONS
where FLEX_LOT_ID = panel_lot_id
group by FLEX_LOT_ID, FLEX_PANEL_DMX;
--Add information about the machine ID and the upload time to this table.
insert into PANEL_STATUS_2 (FLEX_LOT_ID, FLEX_PANEL_DMX, FLEX_PANEL_STATUS, MACHINE_ID, UPLOAD_TIME)
select distinct PANEL_STATUS_1.*, MACHINE_ID, UPLOAD_TIME
from PANEL_STATUS_1, FLEX_PANEL_INSPECTIONS
where (FLEX_PANEL_INSPECTIONS.FLEX_LOT_ID = PANEL_STATUS_1.FLEX_LOT_ID
and FLEX_PANEL_INSPECTIONS.FLEX_PANEL_DMX = PANEL_STATUS_1.FLEX_PANEL_DMX)
and FLEX_PANEL_INSPECTIONS.FLEX_LOT_ID = panel_lot_id;
end PANEL_STATUS_PROCEDURE;
/
答案 0 :(得分:3)
您可以将临时表创建为
create global temporary table gtt_panel_status
( column datatype ... )
on commit [delete|preserve] rows;
(在delete
子句中指定preserve
或on commit
。
但是你通常不需要临时表。您可以尝试with
子句(CTE),或沿select x, y, z from (select your subquery here)
行的内联视图。
编辑:实际上更多地查看您的查询,我认为您实际需要的是分析sum
,即没有聚合的总计。例如,像这样:
create or replace procedure panel_status_procedure
( panel_lot_id in number )
as
begin
-- Add information about the machine ID and the upload time to this table.
insert into panel_status_2
( flex_lot_id
, flex_panel_dmx
, flex_panel_status
, machine_id
, upload_time )
select distinct
flex_lot_id
, flex_panel_dmx
, case sum(flex_panel_status) over (partition by flex_lot_id, flex_panel_dmx)
when 4 then 1
else 0
end
, machine_id
, upload_time
from flex_panel_inspections pi
where pi.flex_lot_id = panel_lot_id;
end panel_status_procedure;