sales_walk/sql/schema.pg.sql

304 lines
8.4 KiB
MySQL
Raw Permalink Normal View History

2023-10-11 11:39:13 -04:00
/*
walk
sales_walk_agg
sales_walk
sales_walk_seg_agg
sales_walk_seg
*/
2023-10-18 15:00:33 -04:00
CREATE TABLE IF NOT EXISTS rlarp.walk_r1 (
2023-10-24 16:22:00 -04:00
bill_cust text
,ship_cust text
,bucket text
2023-10-18 15:00:33 -04:00
,attainment numeric
2023-10-24 16:22:00 -04:00
,notes text
,fcnotes text
2023-10-18 15:00:33 -04:00
,PRIMARY KEY (bill_cust, ship_cust)
2023-09-01 08:29:11 -04:00
);
2023-10-18 15:00:33 -04:00
GRANT ALL ON TABLE rlarp.walk_r1 TO PUBLIC;
DROP VIEW IF EXISTS rlarp.sales_walk_r1_seg;
DROP VIEW IF EXISTS rlarp.sales_walk_r1;
2023-10-11 11:39:13 -04:00
------------------------------------------sales walk agg---------------------------------------------------
2023-10-18 15:00:33 -04:00
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_r1_agg ;
2023-10-11 11:39:13 -04:00
2023-10-18 15:00:33 -04:00
CREATE MATERIALIZED VIEW rlarp.sales_walk_r1_agg AS
2023-10-11 11:39:13 -04:00
WITH
2023-10-17 16:06:42 -04:00
act as (
2023-10-11 11:39:13 -04:00
SELECT
bill_dba
,ship_dba
,dsm
--,os.glec
--,m.biggroup
--,os.priceg
2024-01-31 16:58:20 -05:00
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '12'),0) "Actual 2023"
2023-10-18 15:00:33 -04:00
,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"
2023-10-11 11:39:13 -04:00
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
)
2023-10-17 16:06:42 -04:00
,bgt AS (
SELECT
bill_dba
,ship_dba
,dsm
2023-10-18 15:00:33 -04:00
,sum(sales_usd) sales
,sum(pounds) pounds
2023-10-17 16:06:42 -04:00
--string_agg(distinct mold,', ')
2023-10-18 15:00:33 -04:00
FROM
rlarp.osm_stack
WHERE
version = 'Budget'
AND oseas = '2024'
2023-10-17 16:06:42 -04:00
GROUP BY
2023-10-18 15:00:33 -04:00
bill_dba
,ship_dba
,dsm
2023-10-17 16:06:42 -04:00
)
,agg AS (
SELECT
2023-10-18 15:00:33 -04:00
COALESCE(act.bill_dba,TRIM(bgt.bill_dba)) bill_dba
,COALESCE(act.ship_dba,TRIM(bgt.ship_dba)) ship_dba
,COALESCE(act.dsm ,TRIM(bgt.dsm )) dsm
2023-10-24 16:22:00 -04:00
,COALESCE(SUM(bgt.pounds ),0) "Budget 2024"
,COALESCE(SUM(act."Actual 2023"),0) "Actual 2023"
,COALESCE(SUM(act."Actual 2024"),0) "Actual 2024"
,COALESCE(SUM(act."Open Ord" ),0) "Open Ord"
,COALESCE(SUM(act."Quotes" ),0) "Quotes"
2023-10-18 15:00:33 -04:00
FROM
act
FULL OUTER JOIN bgt ON
TRIM(bgt.bill_dba) = act.bill_dba
AND TRIM(bgt.ship_dba) = act.ship_dba
AND TRIM(bgt.dsm ) = act.dsm
GROUP BY
COALESCE(act.bill_dba,TRIM(bgt.bill_dba))
,COALESCE(act.ship_dba,TRIM(bgt.ship_dba))
,COALESCE(act.dsm ,TRIM(bgt.dsm ))
2023-10-17 16:06:42 -04:00
)
2023-10-18 15:00:33 -04:00
SELECT * FROM agg;
2023-10-11 11:39:13 -04:00
------------------------------------------sales walk-------------------------------------------------------
2023-10-18 15:00:33 -04:00
CREATE OR REPLACE VIEW rlarp.sales_walk_r1 AS
2023-10-11 11:39:13 -04:00
SELECT
bill_dba
,ship_dba
,dsm
--,glec
--,biggroup
--,priceg
2023-10-18 15:00:33 -04:00
,"Budget 2024" budget_2024
2023-10-11 11:39:13 -04:00
,"Actual 2023" shipments_2023
,"Actual 2024" shipments_2024
,"Open Ord" open_orders_2024
,"Quotes" quotes
,CASE WHEN "Actual 2023" > 0
THEN CASE
2023-10-24 16:22:00 -04:00
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted'
2023-10-11 11:39:13 -04:00
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'
2023-10-24 16:22:00 -04:00
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted'
2023-10-11 11:39:13 -04:00
END
END flag
2023-10-18 15:00:33 -04:00
,COALESCE(w.bucket,a.bucket,'None') bucket
,COALESCE(w.attainment,0) attainment
,COALESCE(w.notes,a.notes,'-') notes
2023-10-24 16:22:00 -04:00
,COALESCE(w.fcnotes,'-') fcnotes
2023-10-11 11:39:13 -04:00
FROM
2023-10-18 15:00:33 -04:00
rlarp.sales_walk_r1_agg agg
LEFT OUTER JOIN rlarp.walk a ON
a.ship_cust = agg.ship_dba
LEFT OUTER JOIN rlarp.walk_r1 w ON
2023-10-11 11:39:13 -04:00
w.ship_cust = agg.ship_dba
2023-10-18 15:00:33 -04:00
AND w.bill_cust = agg.bill_dba
2023-10-11 11:39:13 -04:00
WHERE
2023-10-18 15:00:33 -04:00
"Budget 2024" <> 0
OR "Actual 2023" <> 0
2023-10-11 11:39:13 -04:00
OR ("Actual 2024") <> 0
OR ("Open Ord") <> 0
OR ("Quotes") <> 0
ORDER BY
dsm
2023-10-18 15:00:33 -04:00
,"Budget 2024" + "Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
2023-10-11 11:39:13 -04:00
2023-10-18 15:00:33 -04:00
GRANT SELECT ON rlarp.sales_walk_r1 TO PUBLIC;
2023-10-11 11:39:13 -04:00
--,biggroup
------------------------------------------sales walk seg agg-----------------------------------------------
2023-10-18 15:00:33 -04:00
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_r1_seg_agg;
2023-10-11 11:39:13 -04:00
2023-10-18 15:00:33 -04:00
CREATE MATERIALIZED VIEW rlarp.sales_walk_r1_seg_agg AS
2023-10-11 11:39:13 -04:00
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
2023-10-18 15:00:33 -04:00
,COALESCE(sum(pounds) filter (WHERE oseas = 2024 AND version = 'Budget'),0) "Budget 2024"
2024-01-31 16:58:20 -05:00
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '12'),0) "Actual 2023"
2023-10-11 11:39:13 -04:00
,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%')
2023-10-18 15:00:33 -04:00
OR (version = 'Budget')
2023-10-11 11:39:13 -04:00
)
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---------------------------------------------------
2023-10-18 15:00:33 -04:00
CREATE OR REPLACE VIEW rlarp.sales_walk_r1_seg AS
2023-10-11 11:39:13 -04:00
SELECT
agg.bill_dba
,agg.ship_dba
,agg.dsm
,agg.director
,agg.nursery_region
,agg.segm
,agg.biggroup
--,priceg
2023-10-18 15:00:33 -04:00
,"Budget 2024" budget_2024
2023-10-11 11:39:13 -04:00
,"Actual 2023" shipments_2023
,"Actual 2024" shipments_2024
,"Open Ord" open_orders_2024
,"Quotes" quotes
2023-10-18 15:00:33 -04:00
,"Budget 2024" * COALESCE(w.attainment,0) available_to_win
2023-10-24 16:22:00 -04:00
,CASE WHEN COALESCE("Actual 2023",0) > 0
2023-10-11 11:39:13 -04:00
THEN CASE
2023-10-24 16:22:00 -04:00
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted'
2023-10-11 11:39:13 -04:00
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'
2023-10-24 16:22:00 -04:00
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'Not Quoted'
ELSE 'Not Quoted'
2023-10-11 11:39:13 -04:00
END
END flag
2023-10-18 15:00:33 -04:00
,COALESCE(w.bucket,a.bucket,'None') bucket
,COALESCE(w.attainment,0) attainment
,COALESCE(w.notes,a.notes,'-') notes
2023-10-24 16:22:00 -04:00
,COALESCE(w.fcnotes,'-') fcnotes
,COALESCE(sw.flag,'Not Quoted') flag_cust
2023-10-11 11:39:13 -04:00
FROM
2023-10-18 15:00:33 -04:00
rlarp.sales_walk_r1_seg_agg agg
LEFT OUTER JOIN rlarp.walk a ON
a.ship_cust = agg.ship_dba
LEFT OUTER JOIN rlarp.walk_r1 w ON
2023-10-11 11:39:13 -04:00
w.ship_cust = agg.ship_dba
2023-10-18 15:00:33 -04:00
AND w.bill_cust = agg.bill_dba
LEFT OUTER JOIN rlarp.sales_walk_r1 sw ON
2023-10-11 11:39:13 -04:00
sw.bill_dba = agg.bill_dba
AND sw.ship_dba = agg.ship_dba
AND sw.dsm = agg.dsm
WHERE
2023-10-18 15:00:33 -04:00
"Budget 2024" <> 0
OR "Actual 2023" <> 0
2023-10-11 11:39:13 -04:00
OR ("Actual 2024") <> 0
OR ("Open Ord") <> 0
OR ("Quotes") <> 0
ORDER BY
dsm
2023-10-18 15:00:33 -04:00
,"Budget 2024" + "Actual 2024" + "Actual 2023" + "Open Ord" + "Quotes" desc;
2023-10-11 11:39:13 -04:00
2023-10-18 15:00:33 -04:00
GRANT SELECT ON rlarp.sales_walk_r1_seg TO PUBLIC;
2023-10-11 11:39:13 -04:00
--,biggroup