sales_walk/sql/pull.sql

91 lines
2.3 KiB
MySQL
Raw Normal View History

2023-09-01 08:29:11 -04:00
DROP VIEW IF EXISTS rlarp.sales_walk;
2023-09-11 11:00:44 -04:00
DROP MATERIALIZED VIEW IF EXISTS rlarp.sales_walk_agg ;
2023-09-01 08:29:11 -04:00
CREATE MATERIALIZED VIEW rlarp.sales_walk_agg AS
WITH
agg as (
SELECT
bill_dba
,ship_dba
,dsm
--,os.glec
--,m.biggroup
--,os.priceg
2023-09-11 11:00:44 -04:00
,COALESCE(sum(pounds) filter (WHERE sseas = 2023 AND version = 'Actual' AND SUBSTRING(smon,1,2) <= '08'),0) "Actual 2023"
2023-09-01 08:29:11 -04:00
,COALESCE(sum(pounds) filter (WHERE sseas = 2024 AND version = 'Actual'),0) "Actual 2024"
2023-09-11 11:00:44 -04:00
,COALESCE(sum(pounds) filter (WHERE version = 'Actual' AND ostatus LIKE 'Open%' AND rseas <= 2024),0) "Open Ord"
2023-09-01 08:29:11 -04:00
,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
2023-09-11 11:00:44 -04:00
,"Open Ord" open_orders_2024
2023-09-01 08:29:11 -04:00
,"Quotes" quotes
,CASE WHEN "Actual 2023" > 0
THEN CASE
WHEN "Actual 2024" = 0 AND "Open Ord" = 0 AND "Quotes" = 0 THEN 'No Activity'
2023-09-11 11:00:44 -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'
2023-09-01 08:29:11 -04:00
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