我有以下查询,其中我正在创建一个临时表来从不同的表中提取和插入数据,并在循环中选择记录。
DO $$
DECLARE
--Only transactions found between the given start date and end date for the given events will be considered
START_TIME_STR VARCHAR := '2018-01-28 00:00:00';
END_TIME_STR VARCHAR := '2018-02-27 23:59:59';
v_category INTEGER := 2;
BEGIN
CREATE TEMPORARY TABLE cntr_track_detail (
cntr_no VARCHAR(11) NOT NULL,
cntr_trxn_id BIGSERIAL NOT NULL,
surajbari TEXT default null,
mokha TEXT default null,
gate_in TEXT default null,
port_in TEXT default null,
terminal_in TEXT default null,
terminal_name VARCHAR(100) default null
) ON COMMIT DROP;
CREATE TEMPORARY TABLE tranx1 ON COMMIT DROP AS select b.* from
(SELECT ctm.cntr_no,ctm.cntr_trxn_id FROM cntr_trxn_mapping ctm INNER JOIN cntr_track_time_log cttl
ON cttl.cntr_trxn_id = ctm.cntr_trxn_id AND ctm.cntr_cycle_id = v_category
AND cttl.timestamp_gnrl >= start_time AND cttl.timestamp_gnrl <= end_time AND cttl.event_id in (13,21,1,19))b;
raise notice 'size of tranx1 is,%', (select count(t.*) from tranx1 t);
FOR v_intrim_loop IN (select distinct(t.*) from tranx1 t)
LOOP
BEGIN
INSERT INTO cntr_track_detail (cntr_no, cntr_trxn_id, surajbari, mokha, gate_in, port_in, terminal_in, terminal_name) VALUES (v_intrim_loop.cntr_no, v_intrim_loop.cntr_trxn_id,
(SELECT x from something LIMIT 1),
(SELECT x from something LIMIT 1),
(SELECT x from something LIMIT 1),
(SELECT x from something LIMIT 1),
(SELECT x from something LIMIT 1),
(SELECT x from something LIMIT 1)
);
raise notice 'iteration ,%',iter;
iter := iter +1;
END;
END LOOP;
END $$;
SELECT a.* FROM cntr_track_detail a ORDER BY a.cntr_no;
我想问一下,如果子查询什么都不返回,那么插入这种方式是否会导致迭代失败。感谢。
答案 0 :(得分:1)
当您使用OrgRelations
删除重复值时,可能您的表中包含重复值,因此会删除这些值,这就是为什么您的行数减少的原因。