我构建了一个插入1个样本的函数,然后插入4个组,执行一些操作并调用另一个函数,最后使用其余数据更新原始样本。
这是功能:
-- Fill time dimension from timestamp
CREATE OR REPLACE FUNCTION build_sample() RETURNS void AS $$
DECLARE
--CONSTANTS
c_short CONSTANT INTERVAL := '5 minutes';
c_medium CONSTANT INTERVAL := '20 minutes';
c_long CONSTANT INTERVAL := '120 minutes';
c_samplePeriod CONSTANT int := 86400;
size int;
startDateTime timestamp;
endDateTime timestamp;
timeDimensionId int;
weatherId int;
this_taxiid int;
this_sampleid int;
this_quickstops int;
this_moderatestops int;
this_longstops int;
stationarytime int;
movingtime int;
result int;
BEGIN
startDateTime := '2008-02-06 00:00:00';
endDateTime := startDateTime + INTERVAL '1 second' * c_samplePeriod;
this_taxiid := 366;
weatherId:=254;
-- Get timeid
Select distinct "refidHour" from "TimeDimension" into timeDimensionId
where timestamp=startDateTime;
-- Insert basic data in a sample
INSERT INTO "Samples" ("period","refidTaxi","refidTime","refidWeather")
VALUES(c_samplePeriod,this_taxiid,timeDimensionId,weatherId)
RETURNING sampleid into this_sampleid;
RAISE NOTICE 'sampleid=%',this_sampleid;
--Sample moving group
Insert into "EntryGroup" Values(this_sampleid,1);
--Sample shortstops group
Insert into "EntryGroup" Values(this_sampleid,2);
--Sample mediumstops group
Insert into "EntryGroup" Values(this_sampleid,3);
--Sample longstops group
Insert into "EntryGroup" Values(this_sampleid,4);
Select build_stops(this_taxiid, startDateTime, this_sampleid);
--#Quick stops
Select count(*) into this_quickstops from "EntryGroup"
where "refidSample" =this_sampleid and "refidType"=2;
--#Moderate stops
Select count(*) into this_moderatestops from "EntryGroup"
where "refidSample"=this_sampleid and "refidType"=3;
--#Long stops
Select count(*) into this_longstops from "EntryGroup"
where "refidSample"=this_sampleid and "refidType"=4;
RAISE NOTICE 'quickstops=% moderatestops=% longstops=%',this_quickstops, this_moderatestops, this_longstops;
--Calculate stationary time and moving time
Select this_quickstops*c_short+this_moderatestops*c_medium+this_longstops*c_long into stationarytime;
Select c_samplePeriod-stationarytime into movingtime;
RAISE NOTICE 'statironarytime=% movingtime=%',statironarytime, movingtime;
-- Update sample with rest of data
UPDATE "Samples" SET ("stationarytime","movingtime","quickstops","moderatestops","longstops") = (stationarytime,movingtime,this_quickstops,this_moderatestops,this_longstops)
where sampleid=this_sampleid;
RAISE NOTICE 'period=% refidTaxi=% refidTime=%',c_samplePeriod,this_taxiid,timeDimensionId;
END;
$$ LANGUAGE plpgsql;
我收到以下错误
snowflake=# select build_sample();
NOTICE: sampleid=3
ERROR: insert or update on table "EntryGroup" violates foreign key constraint "fk_Entries_has_Samples_Samples1"
DETAIL: Key (refidSample)=(1) is not present in table "Samples".
CONTEXT: SQL statement "Insert into "EntryGroup" Values(this_sampleid,1)"
当函数尝试插入包含上一个插入样本的sampleid的组时,会出现问题。
有什么问题可以实现我正在做的事情,我的意思是,因为交易还没有完成,这是否意味着样本没有真正插入,我不能为该样本插入一个组?
答案 0 :(得分:1)
有什么问题可以实现我正在做的事情,我的意思是, 因为交易还没有完成它意味着样本 是不是真的插入了,我不能为该样本插入一个组?
没有。您可能在插入中的列不匹配。
答案 1 :(得分:1)
始终为持久性INSERT
语句提供目标列表(少数例外情况适用)。在您的函数中,替换:
--Sample moving group
Insert into "EntryGroup" Values(this_sampleid,1);
--Sample shortstops group
Insert into "EntryGroup" Values(this_sampleid,2);
--Sample mediumstops group
Insert into "EntryGroup" Values(this_sampleid,3);
--Sample longstops group
Insert into "EntryGroup" Values(this_sampleid,4);
有了这个:
INSERT INTO "EntryGroup"("refidSample", "refidType")
SELECT this_sampleid, i.type
FROM (VALUES (1), (2), (3), (4)) i(type);
从其余代码中猜测列名。使用实际目标列名称。
在使用它时,我用一个(更便宜的)多排插件替换了你的四个插件。