am cooked boolean won’t turn false in active column [closed]

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>TRUNCATE data_extract_as_is, editing, error_table, running_table, product_table, address_table, date_table, date_hierarchy, product_hierarchy,product_versioning_table;
SELECT * FROM product_table
SELECT * FROM product_hierarchy
SELECT * FROM public.product_versioning_table
CALL insert_product_hierarchy();
CREATE TABLE IF NOT EXISTS product_versioning_table
(
product_id VARCHAR(100) NOT NULL, -- Product ID from product_hierarchy (duplicate allowed)
product VARCHAR NOT NULL, -- Product name
price_each NUMERIC(10, 2) NOT NULL, -- Product price
active BOOLEAN NOT NULL DEFAULT TRUE, -- Automatically set to TRUE
update_from DATE NOT NULL DEFAULT CURRENT_DATE, -- Automatically set to current date
update_until DATE
);
CREATE OR REPLACE FUNCTION update_product_versioning_table()
RETURNS void AS $$
BEGIN
-- Step 1: Mark old records as inactive (close the previous version by setting update_until date)
UPDATE product_versioning_table
SET update_until = CURRENT_DATE, active = FALSE
WHERE product_id IN (
SELECT ph.product_id
FROM running_table rt
JOIN product_hierarchy ph ON rt.product = ph.specific_product)
AND active = TRUE;
-- Step 2: Insert new active versions of products with the updated data
INSERT INTO product_versioning_table (product_id, product, price_each, active, update_from)
SELECT DISTINCT ph.product_id, rt.product, rt.price_each, TRUE, CURRENT_DATE
FROM running_table rt
JOIN product_hierarchy ph ON rt.product = ph.specific_product -- Join on the 'product' field to get 'product_id'
WHERE NOT EXISTS (
SELECT 1
FROM product_versioning_table pvt
WHERE ph.product_id = pvt.product_id AND pvt.active = TRUE);
RAISE NOTICE 'Product versioning table updated successfully.';
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION trigger_product_versioning_update()
RETURNS TRIGGER AS $$
BEGIN
RAISE NOTICE 'Trigger fired on running_table insert.';
PERFORM update_product_versioning_table(); -- Call the updated function
RETURN NULL; -- No return value for statement-level triggers
END;
$$ LANGUAGE plpgsql;
-- Drop existing trigger if exists
DROP TRIGGER IF EXISTS product_versioning_update_trigger ON running_table;
-- Create the trigger for running_table
CREATE TRIGGER product_versioning_update_trigger
AFTER INSERT ON running_table
FOR EACH STATEMENT
EXECUTE FUNCTION trigger_product_versioning_update();
-- First version of the product
INSERT INTO product_versioning_table (product_id, product, price_each)
VALUES('IPp-1', 'iPhone', 1999.99);
-- Second version of the same product with a different price
INSERT INTO product_versioning_table (product_id, product, price_each, active, update_from)
VALUES ('IPp-1', 'iPhone', 1099.99, TRUE, CURRENT_DATE);
</code>
<code>TRUNCATE data_extract_as_is, editing, error_table, running_table, product_table, address_table, date_table, date_hierarchy, product_hierarchy,product_versioning_table; SELECT * FROM product_table SELECT * FROM product_hierarchy SELECT * FROM public.product_versioning_table CALL insert_product_hierarchy(); CREATE TABLE IF NOT EXISTS product_versioning_table ( product_id VARCHAR(100) NOT NULL, -- Product ID from product_hierarchy (duplicate allowed) product VARCHAR NOT NULL, -- Product name price_each NUMERIC(10, 2) NOT NULL, -- Product price active BOOLEAN NOT NULL DEFAULT TRUE, -- Automatically set to TRUE update_from DATE NOT NULL DEFAULT CURRENT_DATE, -- Automatically set to current date update_until DATE ); CREATE OR REPLACE FUNCTION update_product_versioning_table() RETURNS void AS $$ BEGIN -- Step 1: Mark old records as inactive (close the previous version by setting update_until date) UPDATE product_versioning_table SET update_until = CURRENT_DATE, active = FALSE WHERE product_id IN ( SELECT ph.product_id FROM running_table rt JOIN product_hierarchy ph ON rt.product = ph.specific_product) AND active = TRUE; -- Step 2: Insert new active versions of products with the updated data INSERT INTO product_versioning_table (product_id, product, price_each, active, update_from) SELECT DISTINCT ph.product_id, rt.product, rt.price_each, TRUE, CURRENT_DATE FROM running_table rt JOIN product_hierarchy ph ON rt.product = ph.specific_product -- Join on the 'product' field to get 'product_id' WHERE NOT EXISTS ( SELECT 1 FROM product_versioning_table pvt WHERE ph.product_id = pvt.product_id AND pvt.active = TRUE); RAISE NOTICE 'Product versioning table updated successfully.'; END; $$ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION trigger_product_versioning_update() RETURNS TRIGGER AS $$ BEGIN RAISE NOTICE 'Trigger fired on running_table insert.'; PERFORM update_product_versioning_table(); -- Call the updated function RETURN NULL; -- No return value for statement-level triggers END; $$ LANGUAGE plpgsql; -- Drop existing trigger if exists DROP TRIGGER IF EXISTS product_versioning_update_trigger ON running_table; -- Create the trigger for running_table CREATE TRIGGER product_versioning_update_trigger AFTER INSERT ON running_table FOR EACH STATEMENT EXECUTE FUNCTION trigger_product_versioning_update(); -- First version of the product INSERT INTO product_versioning_table (product_id, product, price_each) VALUES('IPp-1', 'iPhone', 1999.99); -- Second version of the same product with a different price INSERT INTO product_versioning_table (product_id, product, price_each, active, update_from) VALUES ('IPp-1', 'iPhone', 1099.99, TRUE, CURRENT_DATE); </code>
TRUNCATE data_extract_as_is, editing, error_table, running_table, product_table, address_table, date_table, date_hierarchy, product_hierarchy,product_versioning_table;

SELECT * FROM product_table
SELECT * FROM product_hierarchy
SELECT * FROM public.product_versioning_table

CALL insert_product_hierarchy();

CREATE TABLE IF NOT EXISTS product_versioning_table 
(
    product_id VARCHAR(100) NOT NULL,  -- Product ID from product_hierarchy (duplicate allowed)
    product VARCHAR NOT NULL,          -- Product name
    price_each NUMERIC(10, 2) NOT NULL, -- Product price
    active BOOLEAN NOT NULL DEFAULT TRUE,  -- Automatically set to TRUE
    update_from DATE NOT NULL DEFAULT CURRENT_DATE, -- Automatically set to current date
    update_until DATE
);

CREATE OR REPLACE FUNCTION update_product_versioning_table()
RETURNS void AS $$
BEGIN
    -- Step 1: Mark old records as inactive (close the previous version by setting update_until date)
    UPDATE product_versioning_table
    SET update_until = CURRENT_DATE, active = FALSE
    WHERE product_id IN (
        SELECT ph.product_id
        FROM running_table rt
        JOIN product_hierarchy ph ON rt.product = ph.specific_product)
    AND active = TRUE;

    -- Step 2: Insert new active versions of products with the updated data
    INSERT INTO product_versioning_table (product_id, product, price_each, active, update_from)
    SELECT DISTINCT ph.product_id, rt.product, rt.price_each, TRUE, CURRENT_DATE
    FROM running_table rt
    JOIN product_hierarchy ph ON rt.product = ph.specific_product  -- Join on the 'product' field to get 'product_id'
    WHERE NOT EXISTS (
        SELECT 1
        FROM product_versioning_table pvt
        WHERE ph.product_id = pvt.product_id AND pvt.active = TRUE);

    RAISE NOTICE 'Product versioning table updated successfully.';
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION trigger_product_versioning_update()
RETURNS TRIGGER AS $$
BEGIN
    RAISE NOTICE 'Trigger fired on running_table insert.';
    PERFORM update_product_versioning_table(); -- Call the updated function
    RETURN NULL; -- No return value for statement-level triggers
END;
$$ LANGUAGE plpgsql;


-- Drop existing trigger if exists
DROP TRIGGER IF EXISTS product_versioning_update_trigger ON running_table;
-- Create the trigger for running_table
CREATE TRIGGER product_versioning_update_trigger
AFTER INSERT ON running_table
FOR EACH STATEMENT
EXECUTE FUNCTION trigger_product_versioning_update();

-- First version of the product
INSERT INTO product_versioning_table (product_id, product, price_each)
VALUES('IPp-1', 'iPhone', 1999.99);

-- Second version of the same product with a different price
INSERT INTO product_versioning_table (product_id, product, price_each, active, update_from)
VALUES ('IPp-1', 'iPhone', 1099.99, TRUE, CURRENT_DATE);

I need my active to return false when there is a duplicate product_id

New contributor

Nathaniel Ulibarri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật