
DROP TABLE IF EXISTS pricequote.core_target;

CREATE TABLE pricequote.core_target (
    compset TEXT        NOT NULL,
    stlc    TEXT        NOT NULL,
    floor   NUMERIC     NOT NULL,
    options JSONB       NOT NULL,
    PRIMARY KEY (stlc)
);

GRANT SELECT, INSERT, UPDATE, DELETE ON pricequote.target_prices TO PUBLIC;

DROP TABLE IF EXISTS import.core_target;

CREATE TABLE import.core_target AS (SELECT * FROM pricequote.core_target);

WITH
expand AS (
SELECT
    c.compset,
    c.stlc,
    c.floor,
    b.ds,
    b.chan,
    b.tier,
    b.vol,
    b.val,
    b.price,
    json_pretty(to_json(b.math)) math
FROM
    pricequote.core_target c
    LEFT JOIN LATERAL pricequote.build_pricing_path_base (options||jsonb_build_object('entity','Anchor','attr',c.stlc,'val',c.floor,'func','Price')) b ON b.lastflag
)
-- select count(*) from expand
INSERT INTO 
    pricequote.target_prices_base
SELECT  
    *
FROM 
    expand
