所以,我正在尝试创建一个trigger
来接收来自insert
操作的一些值,join
使用一些表,然后insert
将结果导入另一个表
我不知道查询对于触发器是否过于复杂,但它不断引发错误:
ERROR: syntax error at or near "CREATE"
LINE 2: CREATE OR REPLACE FUNCTION TG_PORTFOLIO() RETURNS TRIGGER
代码在下面,如果有人可以帮助我,我会很高兴。
CREATE OR REPLACE FUNCTION TG_PORTFOLIO() RETURNS TRIGGER AS $TG_PORTFOLIO$
BEGIN
WITH LV_MONTHLY_REVENUE AS (
SELECT
PTF1.ENTERPRISE_ID, PTF1.MONTHLY_REVENUE_CURRENT
FROM
BETA_PORTFOLIO PTF1
LEFT JOIN BETA_PORTFOLIO PTF2
ON (PTF1.ENTERPRISE_ID = PTF2.ENTERPRISE_ID AND PTF1._ID < PTF2._ID)
WHERE
PTF2._ID IS NULL
),
LV_MONTHLY_RECURRENCE AS (
SELECT
PTF1.ENTERPRISE_ID, PTF1.MONTHLY_RECURRENCE_CURRENT
FROM
BETA_PORTFOLIO PTF1
LEFT JOIN BETA_PORTFOLIO PTF2
ON (PTF1.ENTERPRISE_ID = PTF2.ENTERPRISE_ID AND PTF1._ID < PTF2._ID)
WHERE
PTF2._ID IS NULL
),
LV_MONTHLY_COUPON AS (
SELECT
PTF1.ENTERPRISE_ID, PTF1.MONTHLY_COUPON_CURRENT
FROM
BETA_PORTFOLIO PTF1
LEFT JOIN BETA_PORTFOLIO PTF2
ON (PTF1.ENTERPRISE_ID = PTF2.ENTERPRISE_ID AND PTF1._ID < PTF2._ID)
WHERE
PTF2._ID IS NULL
),
LV_SEATS AS (
SELECT
PTF1.ENTERPRISE_ID, PTF1.SEATS_CURRENT
FROM
BETA_PORTFOLIO PTF1
LEFT JOIN BETA_PORTFOLIO PTF2
ON (PTF1.ENTERPRISE_ID = PTF2.ENTERPRISE_ID AND PTF1._ID < PTF2._ID)
WHERE
PTF2._ID IS NULL
)
INSERT INTO
BETA_PORTFOLIO (ENTERPRISE_ID, SUBSCRIPTION_ID, ENTERPRISE_LIFEYCLE_ID, PAYMENT_METHOD, CREATAD_AT, EVENT_TYPE, RECURRENCE_FREQUENCY, PLAN_ID, MONTHLY_REVENUE, MONTHLY_REVENUE_CURRENT, MONTHLY_RECURRENCE, MONTHLY_RECURRENCE_CURRENT, MONTHLY_COUPON, MONTHLY_COUPON_CURRENT, SEATS, SEATS_CURRENT , HAS_COUPON, TEAMS_CORPORATE)
VALUES ( NEW.ENTERPRISE_ID, NEW.SUBSCRIPTION_ID, NEW.ID, NEW.PAYMENT_METHOD, NEW.CREATED_AT, NEW.EVENT_TYPE, NEW.RECURRENCE_FREQUENCY, NEW.PLAN_ID,
/*monthly_revenue*/
(SELECT MONTHLY_FEE_BRL FROM PLANS WHERE ID = NEW.NEW_PLAN_ID),
/*monthly_revenue_current*/
(SELECT MONTHLY_FEE_BRL FROM PLANS WHERE ID = NEW.NEW_PLAN_ID) +
CASE WHEN
(SELECT MONTHLY_REVENUE_CURRENT FROM LV_MONTHLY_REVENUE WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID) IS NULL THEN 0
ELSE
(SELECT MONTHLY_REVENUE_CURRENT FROM LV_MONTHLY_REVENUE WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID)
END,
/*monthly_recurrence*/
(SELECT
CASE WHEN NEW.FREQUENCY = 1 THEN PLANS.MONTHLY_FEE_BRL
WHEN NEW.FREQUENCY = 6 THEN PLANS.SEMIANNUALLY_FEE_BRL
WHEN NEW.FREQUENCY = 12 THEN PLANS.ANNUALLY_FEE_BRL
END
FROM
PLANS
WHERE
ID = NEW.NEW_PLAN_ID),
/*monthly_recurrence_current*/
(SELECT
CASE WHEN NEW.FREQUENCY = 1 THEN PLANS.MONTHLY_FEE_BRL
WHEN NEW.FREQUENCY = 6 THEN PLANS.SEMIANNUALLY_FEE_BRL
WHEN NEW.FREQUENCY = 12 THEN PLANS.ANNUALLY_FEE_BRL
END
FROM
PLANS
WHERE
ID = NEW.NEW_PLAN_ID) +
CASE WHEN
(SELECT MONTHLY_RECURRENCE_CURRENT FROM LV_MONTHLY_RECURRENCE WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID) IS NULL THEN 0 ELSE
(SELECT MONTHLY_RECURRENCE_CURRENT FROM LV_MONTHLY_RECURRENCE WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID)
END,
/*monthly_coupon*/
(SELECT
CASE WHEN SUBSCRIPTIONS.RECURRENCE_FREQUENCY = 1 THEN (PLANS.MONTHLY_FEE_BRL * ((100 - (CASE WHEN COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT IS NULL OR RECEIPTS.ORIGINAL_AMOUNT - RECEIPTS.DISCOUNTED_AMOUNT = 0 THEN 0 ELSE COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT END ) )/100))::INTEGER
WHEN SUBSCRIPTIONS.RECURRENCE_FREQUENCY = 6 THEN (PLANS.SEMIANNUALLY_FEE_BRL * ((100 - (CASE WHEN COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT IS NULL OR RECEIPTS.ORIGINAL_AMOUNT - RECEIPTS.DISCOUNTED_AMOUNT = 0 THEN 0 ELSE COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT END ) )/100::FLOAT)/6)::INTEGER
WHEN SUBSCRIPTIONS.RECURRENCE_FREQUENCY = 12 THEN (PLANS.ANNUALLY_FE8E_BRL * ((100 - (CASE WHEN COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT IS NULL OR RECEIPTS.ORIGINAL_AMOUNT - RECEIPTS.DISCOUNTED_AMOUNT = 0 THEN 0 ELSE COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT END ) )/100::FLOAT)/12)::INTEGER
END
FROM SUBSCRIPTIONS
LEFT JOIN PLANS ON PLANS.ID = SUBSCRIPTIONS.ID
LEFT JOIN COUPON_INSTANCES ON COUPON_INSTANCES.SUBSCRIPTION_ID = SUBSCRIPTIONS.ID
LEFT JOIN RECEIPTS ON RECEIPTS.SUBSCRIPTION_ID = SUBSCRIPTIONS.ID
WHERE SUBSCRIPTIONS.ID = NEW.NEW_SUBSCRIPTION_ID
),
/*monthly_coupon_current*/
(SELECT
CASE WHEN SUBSCRIPTIONS.RECURRENCE_FREQUENCY = 1 THEN (PLANS.MONTHLY_FEE_BRL * ((100 - (CASE WHEN COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT IS NULL OR RECEIPTS.ORIGINAL_AMOUNT - RECEIPTS.DISCOUNTED_AMOUNT = 0 THEN 0 ELSE COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT END ) )/100))::INTEGER
WHEN SUBSCRIPTIONS.RECURRENCE_FREQUENCY = 6 THEN (PLANS.SEMIANNUALLY_FEE_BRL * ((100 - (CASE WHEN COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT IS NULL OR RECEIPTS.ORIGINAL_AMOUNT - RECEIPTS.DISCOUNTED_AMOUNT = 0 THEN 0 ELSE COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT END ) )/100::FLOAT)/6)::INTEGER
WHEN SUBSCRIPTIONS.RECURRENCE_FREQUENCY = 12 THEN (PLANS.ANNUALLY_FE8E_BRL * ((100 - (CASE WHEN COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT IS NULL OR RECEIPTS.ORIGINAL_AMOUNT - RECEIPTS.DISCOUNTED_AMOUNT = 0 THEN 0 ELSE COUPON_INSTANCES.PERCENTAGE_OF_DISCOUNT END ) )/100::FLOAT)/12)::INTEGER
END
FROM SUBSCRIPTIONS
LEFT JOIN PLANS ON PLANS.ID = SUBSCRIPTIONS.ID
LEFT JOIN COUPON_INSTANCES ON COUPON_INSTANCES.SUBSCRIPTION_ID = SUBSCRIPTIONS.ID
LEFT JOIN RECEIPTS ON RECEIPTS.SUBSCRIPTION_ID = SUBSCRIPTIONS.ID
WHERE SUBSCRIPTIONS.ID = NEW.NEW_SUBSCRIPTION_ID
) +
CASE WHEN
(SELECT MONTHLY_COUPON_CURRENT FROM LV_MONTHLY_COUPON WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID) IS NULL THEN 0 ELSE
(SELECT MONTHLY_COUPON_CURRENT FROM LV_MONTHLY_COUPON WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID)
END,
/*monthly_coupon_current*/
(select max_users from plans where id = new.new_plan_id),
/*monthly_coupon_current*/
(select max_users from plans where id = new.new_plan_id) +
CASE WHEN
(SELECT SEATS_CURRENT FROM LV_SEATS WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID) IS NULL THEN 0 ELSE
(SELECT SEATS_CURRENT FROM LV_SEATS WHERE ENTERPRISE_ID = NEW.ENTERPRISE_ID)
END,
/*has_coupon*/
(select case when coupon_instances.subscription_id is null then false else true end
from coupon_instances
where subscription_id = new.new_subscription_id),
/*teams_corp*/
select case when select (substring(NEW.new_subscription_id::text,1,1))::integer == 7 and select (substring(NEW.previous_subscription_id::text,1,1))::integer == 6
then true else false end
);
RETURN NULL;
END;
$tg_portfolio$ language plpgsql;