put all the schema in the schema file
This commit is contained in:
parent
37768f3b5d
commit
c1f11d4c8f
60
sql/dev.sql
60
sql/dev.sql
@ -1,60 +0,0 @@
|
||||
------------real history on target scenario------------
|
||||
WITH
|
||||
partcodes AS (
|
||||
SELECT
|
||||
item
|
||||
,'v1:' || COALESCE(i.coltier,'') || '.' || COALESCE(substring(i.branding,1,1),'') || '.' || coalesce(i.uomp,'') || '.' || coalesce(i.suffix,'') || '.' || coalesce(i.accs_ps,'') v1dataseg
|
||||
,_ds.dataseg v0dataseg
|
||||
,part_group
|
||||
FROM
|
||||
"CMS.CUSLG".itemm i
|
||||
LEFT OUTER JOIN rlarp.molds m ON
|
||||
m.stlc = i.stlc
|
||||
LEFT OUTER JOIN _ds ON
|
||||
_ds.colgrp = i.colgrp
|
||||
AND _ds.brand = i.branding
|
||||
WHERE
|
||||
i.item ~ 'ULH12000'
|
||||
AND part_group IS NOT NULL
|
||||
)
|
||||
,customers AS (
|
||||
SELECT
|
||||
dba
|
||||
FROM
|
||||
rlarp.cust c
|
||||
WHERE
|
||||
c.dba ~ 'SUNSHINE GREENH'
|
||||
)
|
||||
,allscenarios AS (
|
||||
SELECT DISTINCT
|
||||
jsonb_build_object('baseitem',part_group,'customer',dba) sc
|
||||
FROM
|
||||
partcodes
|
||||
CROSS JOIN customers
|
||||
)
|
||||
,v1scenarios AS (
|
||||
SELECT DISTINCT
|
||||
jsonb_build_object('baseitem',part_group,'customer',dba,'v1dataseg',v1dataseg) sc
|
||||
FROM
|
||||
partcodes
|
||||
CROSS JOIN customers
|
||||
)
|
||||
,v0scenarios AS (
|
||||
SELECT DISTINCT
|
||||
jsonb_build_object('baseitem',part_group,'customer',dba,'v0dataseg',v0dataseg) sc
|
||||
FROM
|
||||
partcodes
|
||||
CROSS JOIN customers
|
||||
)
|
||||
,hist AS (
|
||||
SELECT
|
||||
s.sc
|
||||
,p.gset
|
||||
,(SELECT string_agg(substring(ok,1,2),'') FROM (SELECT * FROM jsonb_object_keys(p.gset) jok(ok)) x )
|
||||
,p.season
|
||||
FROM
|
||||
allscenarios s
|
||||
LEFT OUTER JOIN rlarp.price_pool_r1 p ON
|
||||
p.gset @> s.sc
|
||||
)
|
||||
SELECT * FROM hist
|
90
sql/pull.sql
90
sql/pull.sql
@ -1,90 +0,0 @@
|
||||
DROP VIEW IF EXISTS rlarp.sales_walk;
|
||||
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_agg ;
|
||||
|
||||
CREATE MATERIALIZED VIEW rlarp.sales_walk_agg AS
|
||||
WITH
|
||||
agg as (
|
||||
SELECT
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
--,os.glec
|
||||
--,m.biggroup
|
||||
--,os.priceg
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes"
|
||||
FROM
|
||||
rlarp.osm_stack os
|
||||
LEFT OUTER JOIN rlarp.molds m ON
|
||||
m.stlc = substring(os.product ,1,8)
|
||||
WHERE
|
||||
(
|
||||
(
|
||||
sseas IN (2023,2024)
|
||||
AND version = 'Actual'
|
||||
AND ostatus = 'Shipped'
|
||||
)
|
||||
OR (
|
||||
version = 'Quotes'
|
||||
AND odate >= '2023-05-01'::date
|
||||
)
|
||||
OR (ostatus LIKE 'Open%')
|
||||
)
|
||||
AND calc_status <> 'CANCELED'
|
||||
AND substring(os.glec,1,1) <= '2'
|
||||
AND os.fs_line = '41010'
|
||||
AND COALESCE(ship_dba,'') <> ''
|
||||
GROUP BY
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
--,os.glec
|
||||
--,m.biggroup
|
||||
--,os.priceg
|
||||
)
|
||||
SELECT * FROM agg;
|
||||
|
||||
CREATE OR REPLACE VIEW rlarp.sales_walk AS
|
||||
SELECT
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
--,glec
|
||||
--,biggroup
|
||||
--,priceg
|
||||
,"Actual 2023" shipments_2023
|
||||
,"Actual 2024" shipments_2024
|
||||
,"Open Ord" open_orders_2024
|
||||
,"Quotes" quotes
|
||||
,CASE WHEN "Actual 2023" > 0
|
||||
THEN CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity'
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Quoted Only'
|
||||
WHEN ("Actual 2024" + "Open Ord") < "Actual 2023" THEN 'Reduced'
|
||||
WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased'
|
||||
END
|
||||
ELSE
|
||||
CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted'
|
||||
WHEN "Actual 2024" > 0 OR "Open Ord" > 0 THEN 'Incremental Won'
|
||||
END
|
||||
END flag
|
||||
,COALESCE(w.bucket,'None') bucket
|
||||
,COALESCE(w.notes,'-') notes
|
||||
FROM
|
||||
rlarp.sales_walk_agg agg
|
||||
LEFT OUTER JOIN rlarp.walk w ON
|
||||
w.ship_cust = agg.ship_dba
|
||||
WHERE
|
||||
"Actual 2023" <> 0
|
||||
OR ("Actual 2024") <> 0
|
||||
OR ("Open Ord") <> 0
|
||||
OR ("Quotes") <> 0
|
||||
ORDER BY
|
||||
dsm
|
||||
,"Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
|
||||
|
||||
GRANT SELECT ON rlarp.sales_walk TO PUBLIC;
|
||||
--,biggroup
|
@ -1,3 +1,13 @@
|
||||
/*
|
||||
walk
|
||||
sales_walk_agg
|
||||
sales_walk
|
||||
sales_walk_seg_agg
|
||||
sales_walk_seg
|
||||
*/
|
||||
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS rlarp.walk (
|
||||
ship_cust text PRIMARY KEY
|
||||
,bucket text
|
||||
@ -5,3 +15,226 @@ CREATE TABLE IF NOT EXISTS rlarp.walk (
|
||||
);
|
||||
|
||||
GRANT ALL ON TABLE rlarp.walk TO PUBLIC;
|
||||
DROP VIEW IF EXISTS rlarp.sales_walk;
|
||||
DROP VIEW IF EXISTS rlarp.sales_walk_seg;
|
||||
|
||||
------------------------------------------sales walk agg---------------------------------------------------
|
||||
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_agg ;
|
||||
|
||||
CREATE MATERIALIZED VIEW rlarp.sales_walk_agg AS
|
||||
WITH
|
||||
agg as (
|
||||
SELECT
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
--,os.glec
|
||||
--,m.biggroup
|
||||
--,os.priceg
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes"
|
||||
FROM
|
||||
rlarp.osm_stack os
|
||||
LEFT OUTER JOIN rlarp.molds m ON
|
||||
m.stlc = substring(os.product ,1,8)
|
||||
WHERE
|
||||
(
|
||||
(
|
||||
sseas IN (2023,2024)
|
||||
AND version = 'Actual'
|
||||
AND ostatus = 'Shipped'
|
||||
)
|
||||
OR (
|
||||
version = 'Quotes'
|
||||
AND odate >= '2023-05-01'::date
|
||||
)
|
||||
OR (ostatus LIKE 'Open%')
|
||||
)
|
||||
AND calc_status <> 'CANCELED'
|
||||
AND substring(os.glec,1,1) <= '2'
|
||||
AND os.fs_line = '41010'
|
||||
AND COALESCE(ship_dba,'') <> ''
|
||||
GROUP BY
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
--,os.glec
|
||||
--,m.biggroup
|
||||
--,os.priceg
|
||||
)
|
||||
SELECT * FROM agg;
|
||||
|
||||
------------------------------------------sales walk-------------------------------------------------------
|
||||
CREATE OR REPLACE VIEW rlarp.sales_walk AS
|
||||
SELECT
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
--,glec
|
||||
--,biggroup
|
||||
--,priceg
|
||||
,"Actual 2023" shipments_2023
|
||||
,"Actual 2024" shipments_2024
|
||||
,"Open Ord" open_orders_2024
|
||||
,"Quotes" quotes
|
||||
,CASE WHEN "Actual 2023" > 0
|
||||
THEN CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity'
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Quoted Only'
|
||||
WHEN ("Actual 2024" + "Open Ord") < "Actual 2023" THEN 'Reduced'
|
||||
WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased'
|
||||
END
|
||||
ELSE
|
||||
CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted'
|
||||
WHEN "Actual 2024" > 0 OR "Open Ord" > 0 THEN 'Incremental Won'
|
||||
END
|
||||
END flag
|
||||
,COALESCE(w.bucket,'None') bucket
|
||||
,COALESCE(w.notes,'-') notes
|
||||
FROM
|
||||
rlarp.sales_walk_agg agg
|
||||
LEFT OUTER JOIN rlarp.walk w ON
|
||||
w.ship_cust = agg.ship_dba
|
||||
WHERE
|
||||
"Actual 2023" <> 0
|
||||
OR ("Actual 2024") <> 0
|
||||
OR ("Open Ord") <> 0
|
||||
OR ("Quotes") <> 0
|
||||
ORDER BY
|
||||
dsm
|
||||
,"Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
|
||||
|
||||
GRANT SELECT ON rlarp.sales_walk TO PUBLIC;
|
||||
--,biggroup
|
||||
|
||||
------------------------------------------sales walk seg agg-----------------------------------------------
|
||||
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_seg_agg;
|
||||
|
||||
CREATE MATERIALIZED VIEW rlarp.sales_walk_seg_agg AS
|
||||
WITH
|
||||
SEG AS (
|
||||
SELECT
|
||||
x.GLEC
|
||||
,x.SEGM
|
||||
FROM
|
||||
(
|
||||
VALUES
|
||||
('1CU','Sustainable'),
|
||||
('1SU','Sustainable'),
|
||||
('1GR','Greenhouse'),
|
||||
('1NU','Nursery'),
|
||||
('1RE','Retail'),
|
||||
('2WI','Greenhouse'),
|
||||
('3BM','Other'),
|
||||
('3CO','Other'),
|
||||
('3PE','Other'),
|
||||
('3PP','Other'),
|
||||
('4CO','Other'),
|
||||
('4RA','Other'),
|
||||
('9MI','Other'),
|
||||
('9SA','Other'),
|
||||
('9TO','Other')
|
||||
) X(GLEC, SEGM)
|
||||
)
|
||||
,agg as (
|
||||
SELECT
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
,director
|
||||
,nursery_region
|
||||
,s.segm
|
||||
,m.biggroup
|
||||
--,os.priceg
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes"
|
||||
FROM
|
||||
rlarp.osm_stack os
|
||||
LEFT OUTER JOIN rlarp.molds m ON
|
||||
m.stlc = substring(os.product ,1,8)
|
||||
LEFT OUTER JOIN seg s ON
|
||||
s.glec = os.glec
|
||||
WHERE
|
||||
(
|
||||
(
|
||||
sseas IN (2023,2024)
|
||||
AND version = 'Actual'
|
||||
AND ostatus = 'Shipped'
|
||||
)
|
||||
OR (
|
||||
version = 'Quotes'
|
||||
AND odate >= '2023-05-01'::date
|
||||
)
|
||||
OR (ostatus LIKE 'Open%')
|
||||
)
|
||||
AND calc_status <> 'CANCELED'
|
||||
AND substring(os.glec,1,1) <= '2'
|
||||
AND os.fs_line = '41010'
|
||||
AND COALESCE(ship_dba,'') <> ''
|
||||
GROUP BY
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
,director
|
||||
,nursery_region
|
||||
,s.segm
|
||||
,m.biggroup
|
||||
--,os.priceg
|
||||
)
|
||||
SELECT * FROM agg;
|
||||
|
||||
------------------------------------------sales walk seg---------------------------------------------------
|
||||
CREATE OR REPLACE VIEW rlarp.sales_walk_seg AS
|
||||
SELECT
|
||||
agg.bill_dba
|
||||
,agg.ship_dba
|
||||
,agg.dsm
|
||||
,agg.director
|
||||
,agg.nursery_region
|
||||
,agg.segm
|
||||
,agg.biggroup
|
||||
--,priceg
|
||||
,"Actual 2023" shipments_2023
|
||||
,"Actual 2024" shipments_2024
|
||||
,"Open Ord" open_orders_2024
|
||||
,"Quotes" quotes
|
||||
,CASE WHEN "Actual 2023" > 0
|
||||
THEN CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity'
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Quoted Only'
|
||||
WHEN ("Actual 2024" + "Open Ord") < "Actual 2023" THEN 'Reduced'
|
||||
WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased'
|
||||
END
|
||||
ELSE
|
||||
CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted'
|
||||
WHEN "Actual 2024" > 0 OR "Open Ord" > 0 THEN 'Incremental Won'
|
||||
END
|
||||
END flag
|
||||
,COALESCE(w.bucket,'None') bucket
|
||||
,COALESCE(w.notes,'-') notes
|
||||
,sw.flag flag_cust
|
||||
FROM
|
||||
rlarp.sales_walk_seg_agg agg
|
||||
LEFT OUTER JOIN rlarp.walk w ON
|
||||
w.ship_cust = agg.ship_dba
|
||||
LEFT OUTER JOIN rlarp.sales_walk sw ON
|
||||
sw.bill_dba = agg.bill_dba
|
||||
AND sw.ship_dba = agg.ship_dba
|
||||
AND sw.dsm = agg.dsm
|
||||
WHERE
|
||||
"Actual 2023" <> 0
|
||||
OR ("Actual 2024") <> 0
|
||||
OR ("Open Ord") <> 0
|
||||
OR ("Quotes") <> 0
|
||||
ORDER BY
|
||||
dsm
|
||||
,"Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
|
||||
|
||||
GRANT SELECT ON rlarp.sales_walk_seg TO PUBLIC;
|
||||
--,biggroup
|
||||
|
@ -1,127 +0,0 @@
|
||||
DROP VIEW IF EXISTS rlarp.sales_walk_seg;
|
||||
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_seg_agg;
|
||||
|
||||
CREATE MATERIALIZED VIEW rlarp.sales_walk_seg_agg AS
|
||||
WITH
|
||||
SEG AS (
|
||||
SELECT
|
||||
x.GLEC
|
||||
,x.SEGM
|
||||
FROM
|
||||
(
|
||||
VALUES
|
||||
('1CU','Sustainable'),
|
||||
('1SU','Sustainable'),
|
||||
('1GR','Greenhouse'),
|
||||
('1NU','Nursery'),
|
||||
('1RE','Retail'),
|
||||
('2WI','Greenhouse'),
|
||||
('3BM','Other'),
|
||||
('3CO','Other'),
|
||||
('3PE','Other'),
|
||||
('3PP','Other'),
|
||||
('4CO','Other'),
|
||||
('4RA','Other'),
|
||||
('9MI','Other'),
|
||||
('9SA','Other'),
|
||||
('9TO','Other')
|
||||
) X(GLEC, SEGM)
|
||||
)
|
||||
,agg as (
|
||||
SELECT
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
,director
|
||||
,nursery_region
|
||||
,s.segm
|
||||
,m.biggroup
|
||||
--,os.priceg
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
|
||||
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
|
||||
,COALESCE(sum(pounds) filter (WHERE version = 'Quotes'),0) "Quotes"
|
||||
FROM
|
||||
rlarp.osm_stack os
|
||||
LEFT OUTER JOIN rlarp.molds m ON
|
||||
m.stlc = substring(os.product ,1,8)
|
||||
LEFT OUTER JOIN seg s ON
|
||||
s.glec = os.glec
|
||||
WHERE
|
||||
(
|
||||
(
|
||||
sseas IN (2023,2024)
|
||||
AND version = 'Actual'
|
||||
AND ostatus = 'Shipped'
|
||||
)
|
||||
OR (
|
||||
version = 'Quotes'
|
||||
AND odate >= '2023-05-01'::date
|
||||
)
|
||||
OR (ostatus LIKE 'Open%')
|
||||
)
|
||||
AND calc_status <> 'CANCELED'
|
||||
AND substring(os.glec,1,1) <= '2'
|
||||
AND os.fs_line = '41010'
|
||||
AND COALESCE(ship_dba,'') <> ''
|
||||
GROUP BY
|
||||
bill_dba
|
||||
,ship_dba
|
||||
,dsm
|
||||
,director
|
||||
,nursery_region
|
||||
,s.segm
|
||||
,m.biggroup
|
||||
--,os.priceg
|
||||
)
|
||||
SELECT * FROM agg;
|
||||
|
||||
CREATE OR REPLACE VIEW rlarp.sales_walk_seg AS
|
||||
SELECT
|
||||
agg.bill_dba
|
||||
,agg.ship_dba
|
||||
,agg.dsm
|
||||
,agg.director
|
||||
,agg.nursery_region
|
||||
,agg.segm
|
||||
,agg.biggroup
|
||||
--,priceg
|
||||
,"Actual 2023" shipments_2023
|
||||
,"Actual 2024" shipments_2024
|
||||
,"Open Ord" open_orders_2024
|
||||
,"Quotes" quotes
|
||||
,CASE WHEN "Actual 2023" > 0
|
||||
THEN CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity'
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Quoted Only'
|
||||
WHEN ("Actual 2024" + "Open Ord") < "Actual 2023" THEN 'Reduced'
|
||||
WHEN ("Actual 2024" + "Open Ord") >= "Actual 2023" THEN 'Increased'
|
||||
END
|
||||
ELSE
|
||||
CASE
|
||||
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" > 0 THEN 'Incremental Quoted'
|
||||
WHEN "Actual 2024" > 0 OR "Open Ord" > 0 THEN 'Incremental Won'
|
||||
END
|
||||
END flag
|
||||
,COALESCE(w.bucket,'None') bucket
|
||||
,COALESCE(w.notes,'-') notes
|
||||
,sw.flag flag_cust
|
||||
FROM
|
||||
rlarp.sales_walk_seg_agg agg
|
||||
LEFT OUTER JOIN rlarp.walk w ON
|
||||
w.ship_cust = agg.ship_dba
|
||||
LEFT OUTER JOIN rlarp.sales_walk sw ON
|
||||
sw.bill_dba = agg.bill_dba
|
||||
AND sw.ship_dba = agg.ship_dba
|
||||
AND sw.dsm = agg.dsm
|
||||
WHERE
|
||||
"Actual 2023" <> 0
|
||||
OR ("Actual 2024") <> 0
|
||||
OR ("Open Ord") <> 0
|
||||
OR ("Quotes") <> 0
|
||||
ORDER BY
|
||||
dsm
|
||||
,"Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
|
||||
|
||||
GRANT SELECT ON rlarp.sales_walk_seg TO PUBLIC;
|
||||
--,biggroup
|
Loading…
Reference in New Issue
Block a user