Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH
- -- 1. Organizations
- orgs AS (
- SELECT DISTINCT
- organization_id,business_name,
- lead_organization_id,registration_number,
- ihls_overall_strength,ihls_trade_category,
- multiIf(org_bookings_created = 0, 'No Shipment',
- org_bookings_created = 1 AND last_booking_at >= '2024-11-01','Single Shipment',
- org_bookings_created > 1 AND last_booking_at >= '2024-11-01','Multi Shipment',
- 'Old Shipment'
- ) AS shipment_status,
- multiIf(
- org_enquiries_created = 0, 'No Enquiry',
- org_enquiries_created = 1 AND cs.last_enquiries_created_at >= '2024-11-01', 'Single Enquiry',
- org_enquiries_created > 1 AND cs.last_enquiries_created_at >= '2024-11-01', 'Multi Enquiry',
- 'Old Enquiry'
- ) AS enquiry_status
- FROM kavach.customer_statistics AS cs FINAL
- WHERE account_type IN ('importer_exporter','importer','exporter')
- AND org_status != 'inactive'
- AND organization_id != '00000000-0000-0000-0000-000000000000'
- ),
- stakeholders as
- (
- SELECT
- DISTINCT cusv.organization_id,
- CASE
- WHEN
- countIf(
- cusv.contact_type = 'contact_number'
- AND cusv.contact_country_code LIKE '%91%'
- AND cusv.contact_stage != 'system_invalidated'
- ) > 0
- THEN 'Yes' ELSE 'No'
- END AS ind_contact,
- max(cusv.sales_agent_id) AS user_id,
- max(cusv.sales_agent) AS cse
- FROM kavach.customer_user_statistics_v2 AS cusv final
- GROUP BY cusv.organization_id
- ),
- shipments AS (
- SELECT
- lead_id,
- registration_number,
- shipment_year,
- incoterm,
- shipment_mode,
- SUM(shipment_count) AS overall,
- SUMIf(shipment_count, origin_country_id = '541d1232-58ce-4d64-83d6-556a42209eb7' AND LOWER(shipment_mode) = 'sea') AS ocean_outbound,
- SUMIf(shipment_count, LOWER(shipment_mode) = 'air') AS air_outbound_shipments,
- SUMIf(shipment_count, origin_country_id IN (
- '1b94734e-7d51-4e94-9dd2-ef96aee64a8f', '177fcbad-8ef7-4324-871c-6c31745f4411',
- '61a683f3-128b-4193-98f7-dd72f68db03d', '6e18d508-87b9-4e7e-a785-b47edc76b0b7',
- '7552270b-caae-4715-acd5-1da117c5434f', '2693fa76-6539-410d-a0b0-551d9e620ba3'
- ) AND LOWER(shipment_mode) = 'air' AND destination_country_id = '541d1232-58ce-4d64-83d6-556a42209eb7') AS east_asia_air,
- SUMIf(shipment_count, origin_country_id IN (
- '1b94734e-7d51-4e94-9dd2-ef96aee64a8f', '177fcbad-8ef7-4324-871c-6c31745f4411',
- '61a683f3-128b-4193-98f7-dd72f68db03d', '6e18d508-87b9-4e7e-a785-b47edc76b0b7',
- '7552270b-caae-4715-acd5-1da117c5434f', '2693fa76-6539-410d-a0b0-551d9e620ba3'
- ) AND LOWER(shipment_mode) = 'sea' AND destination_country_id = '541d1232-58ce-4d64-83d6-556a42209eb7') AS east_asia_sea,
- SUMIf(shipment_count, LOWER(shipment_mode) = 'air' AND (
- -- Meat and Edible Meat Offal
- toString(commodity) LIKE '0201%' OR toString(commodity) LIKE '0202%' OR
- toString(commodity) LIKE '0203%' OR toString(commodity) LIKE '0204%' OR
- toString(commodity) LIKE '0207%' OR toString(commodity) LIKE '0208%' OR toString(commodity) LIKE '0209%' OR
- -- Fish and Seafood
- toString(commodity) LIKE '0301%' OR toString(commodity) LIKE '0302%' OR
- toString(commodity) LIKE '0303%' OR toString(commodity) LIKE '0304%' OR
- toString(commodity) LIKE '0306%' OR toString(commodity) LIKE '0307%' OR
- -- Dairy Products
- toString(commodity) LIKE '0402%' OR toString(commodity) LIKE '0405%' OR toString(commodity) LIKE '0406%' OR
- -- Live Plants
- toString(commodity) LIKE '0602%' OR
- -- Vegetables
- toString(commodity) LIKE '0701%' OR
- toString(commodity) LIKE '0702%' OR
- toString(commodity) LIKE '0703%' OR
- toString(commodity) LIKE '0704%' OR
- toString(commodity) LIKE '0705%' OR
- toString(commodity) LIKE '0706%' OR
- toString(commodity) LIKE '0707%' OR
- toString(commodity) LIKE '0708%' OR
- toString(commodity) LIKE '0709%' OR
- toString(commodity) LIKE '0710%' OR
- toString(commodity) LIKE '0711%' OR
- toString(commodity) LIKE '0712%' OR
- toString(commodity) LIKE '0713%' OR
- toString(commodity) LIKE '0714%' OR
- -- Fruits and Nuts
- toString(commodity) LIKE '0801%' OR
- toString(commodity) LIKE '0802%' OR
- toString(commodity) LIKE '0803%' OR
- toString(commodity) LIKE '0804%' OR
- toString(commodity) LIKE '0805%' OR
- toString(commodity) LIKE '0806%' OR
- toString(commodity) LIKE '0807%' OR
- toString(commodity) LIKE '0808%' OR
- toString(commodity) LIKE '0809%' OR
- toString(commodity) LIKE '0810%' OR
- toString(commodity) LIKE '0811%' OR
- toString(commodity) LIKE '0812%' OR
- toString(commodity) LIKE '0813%' OR
- -- Miscellaneous Edible Preparations
- toString(commodity) LIKE '2106%'
- )) AS air_outbound_perishable_shipments,
- SUMIf(shipment_count,origin_country_id IN ('1b94734e-7d51-4e94-9dd2-ef96aee64a8f')
- AND destination_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')) AS cn_in,
- SUMIf(shipment_count,origin_country_id
- IN ('177fcbad-8ef7-4324-871c-6c31745f4411', --Vietnam
- '61a683f3-128b-4193-98f7-dd72f68db03d', --Thailand
- '6e18d508-87b9-4e7e-a785-b47edc76b0b7', --Singapore
- '7552270b-caae-4715-acd5-1da117c5434f', --Malaysia
- 'bd771cfe-8ffa-4400-85ca-fccfc0ae33f1', --SouthKorea
- 'd9ad9e8c-f1a4-408a-b3fb-f83d12587aaf', --Japan
- '2693fa76-6539-410d-a0b0-551d9e620ba3' --Indonesia
- ) AND destination_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')) AS sea_in,
- SUMIf(shipment_count,origin_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')
- AND destination_country_id IN ('fe92b7c7-9481-4a3b-8d79-df9a7bf94a4e', --US
- 'e8a9b2e8-5fdc-41d0-bb87-3191029649d1' --Canada
- )) AS in_us,
- SUMIf(shipment_count,origin_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')
- AND destination_country_id IN ('345f3aa9-ae78-40cf-b70a-fc5c3af2af99', --UAE
- 'de66d06c-bd96-4a69-8976-81fb6c16538e', --Saudi
- 'cd326552-e99d-4e09-9c6c-372371921fe9', --Kuwait
- '47470b03-5922-4df9-a8a9-0782c3e86074', --Qatar
- '82ac4101-aa13-4cd4-90e1-f3e859656477', --Oman
- '4241f4d4-c791-4cc0-b78f-98efaaf157f6', --Bahrain
- '79d361d6-8157-4df2-9705-a2e17ce14a26' --Sudan
- )) AS in_gulf,
- SUMIf(shipment_count,origin_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')
- AND destination_country_id IN ('3a8af1c5-960c-4765-81e2-d20e3c499c2f', --Kenya
- 'b298143b-2ca1-4dfa-aa64-4151a2498fd9', --Tanzania
- '724a3ef0-6333-4c2a-8019-8c0401356c35', --Uganda
- 'f0455923-373b-4249-a6d3-a27a6a87f215', --Ethiopia
- '790d80b3-ed46-45a2-8510-3f337ec1b7ab' --Mozambique
- )) AS in_eafrica,
- SUMIf(shipment_count,origin_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')
- AND destination_country_id IN ('71bde190-f4ce-464b-b71b-c0935c1308c6', --Nigeria
- 'c1f8e2b3-d5b0-41ba-99b8-4133db701a5d', --Ghana
- 'd7c3b525-dacc-4b50-abc4-ea063d466932', --IvoryCoast
- 'f5e623ba-ab71-46d5-94ae-a4751747d252', --Senegal
- 'c1692c66-bcc6-45a3-8ae8-ef6f95cdbd31', --Togo
- 'acd25c39-19fd-484d-83a9-05363c1b0d4b' --Benin
- )) AS in_wafrica,
- SUMIf(shipment_count,origin_country_id IN ('541d1232-58ce-4d64-83d6-556a42209eb7')
- AND destination_country_id IN ('da61a027-a467-4565-acfb-00783932a191', --Czech Republic
- '60c3faf4-302b-42be-beb0-47f3e12aed5d', --France
- 'd4548d60-b291-4975-97b9-f320af042560', --Germany
- '8114bfac-e913-45ac-aae6-fac3a50d5a25', --Hungary
- '3fef4273-e6f5-4ee6-b125-55a6938fc126', --Ireland
- 'f0c113f4-c65e-429c-be94-046fe5348a92', --Poland
- '53f3d322-b6ed-4335-b113-68a1f637437b', --Slovakia
- 'c0032d9a-4872-4ab0-9cea-369bbdb28604', --Spain
- '222d4b9d-56a8-4580-b761-a71c653263fb' --UK
- )) AS in_europe
- FROM kavach.lead_shipment_commodity_stats
- WHERE shipment_year IN ('2024', '2025')
- GROUP BY lead_id, registration_number, shipment_year, incoterm, shipment_mode
- ),
- -- 3. Aggregation logic by org
- aggregations AS (
- SELECT
- org.organization_id,
- SUMIf(s.overall, s.shipment_year IN ('2024', '2025')) AS overall_shipment,
- SUM(s.east_asia_sea)east_asia_sea,
- SUM(s.east_asia_air)east_asia_air,
- SUM(s.ocean_outbound)ocean_outbound,
- SUM(s.air_outbound_perishable_shipments)air_outbound_perishable_shipments,
- SUM(s.air_outbound_shipments)air_outbound_shipments,
- CASE
- WHEN SUMIf(s.overall, s.shipment_year IN ('2024', '2025')) >= 480 THEN 'Super Large'
- WHEN SUMIf(s.overall, s.shipment_year IN ('2024', '2025')) >= 120 THEN 'Large'
- WHEN SUMIf(s.overall, s.shipment_year IN ('2024', '2025')) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUMIf(s.overall, s.shipment_year IN ('2024', '2025')) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUMIf(s.overall, s.shipment_year IN ('2024', '2025')) BETWEEN 0 AND 9 THEN 'Micro'
- ELSE NULL
- END AS overall_strength,
- CASE
- WHEN SUMIf(s.air_outbound_shipments, s.shipment_year IN ('2024', '2025')) >= 480 THEN 'Super Large'
- WHEN SUMIf(s.air_outbound_shipments, s.shipment_year IN ('2024', '2025')) >= 120 THEN 'Large'
- WHEN SUMIf(s.air_outbound_shipments, s.shipment_year IN ('2024', '2025')) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUMIf(s.air_outbound_shipments, s.shipment_year IN ('2024', '2025')) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUMIf(s.air_outbound_shipments, s.shipment_year IN ('2024', '2025')) BETWEEN 0 AND 9 THEN 'Micro'
- ELSE NULL
- END AS overall_strength_ao,
- CASE
- WHEN SUMIf(s.air_outbound_perishable_shipments, s.shipment_year IN ('2024', '2025')) >= 480 THEN 'Super Large'
- WHEN SUMIf(s.air_outbound_perishable_shipments, s.shipment_year IN ('2024', '2025')) >= 120 THEN 'Large'
- WHEN SUMIf(s.air_outbound_perishable_shipments, s.shipment_year IN ('2024', '2025')) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUMIf(s.air_outbound_perishable_shipments, s.shipment_year IN ('2024', '2025')) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUMIf(s.air_outbound_perishable_shipments, s.shipment_year IN ('2024', '2025')) BETWEEN 0 AND 9 THEN 'Micro'
- ELSE NULL
- END AS overall_strength_ao_p,
- CASE
- WHEN SUMIf(s.east_asia_air, s.shipment_year IN ('2024', '2025')) >= 480 THEN 'Super Large'
- WHEN SUMIf(s.east_asia_air, s.shipment_year IN ('2024', '2025')) >= 120 THEN 'Large'
- WHEN SUMIf(s.east_asia_air, s.shipment_year IN ('2024', '2025')) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUMIf(s.east_asia_air, s.shipment_year IN ('2024', '2025')) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUMIf(s.east_asia_air, s.shipment_year IN ('2024', '2025')) BETWEEN 0 AND 9 THEN 'Micro'
- ELSE NULL
- END AS overall_strength_ea_ai,
- CASE
- WHEN SUMIf(s.east_asia_sea, s.shipment_year IN ('2024', '2025')) >= 480 THEN 'Super Large'
- WHEN SUMIf(s.east_asia_sea, s.shipment_year IN ('2024', '2025')) >= 120 THEN 'Large'
- WHEN SUMIf(s.east_asia_sea, s.shipment_year IN ('2024', '2025')) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUMIf(s.east_asia_sea, s.shipment_year IN ('2024', '2025')) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUMIf(s.east_asia_sea, s.shipment_year IN ('2024', '2025')) BETWEEN 0 AND 9 THEN 'Micro'
- ELSE NULL
- END AS overall_strength_ea_oi,
- CASE
- WHEN SUMIf(s.ocean_outbound, s.shipment_year IN ('2024', '2025')) >= 480 THEN 'Super Large'
- WHEN SUMIf(s.ocean_outbound, s.shipment_year IN ('2024', '2025')) >= 120 THEN 'Large'
- WHEN SUMIf(s.ocean_outbound, s.shipment_year IN ('2024', '2025')) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUMIf(s.ocean_outbound, s.shipment_year IN ('2024', '2025')) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUMIf(s.ocean_outbound, s.shipment_year IN ('2024', '2025')) BETWEEN 0 AND 9 THEN 'Micro'
- ELSE NULL
- END AS overall_strength_oo,
- /* ───────── trade_category ────────── */
- /* ────────────────────────────────── trade_category ───────────────────────────────── */
- CASE
- /* ---------- 0. nothing at all -------------------------------------------------------- */
- WHEN coalesce( SUM(s.east_asia_sea) ,0)
- + coalesce( SUM(s.east_asia_air) ,0)
- + coalesce( SUM(s.ocean_outbound) ,0)
- + coalesce( SUM(s.air_outbound_shipments) ,0)
- + coalesce( SUM(s.air_outbound_perishable_shipments) ,0) = 0
- THEN NULL
- /* ---------- 1. IMPORT volume ≥ OUTBOUND volume -------------------------------------- */
- WHEN coalesce(SUM(s.east_asia_sea),0) + coalesce(SUM(s.east_asia_air),0)
- >= coalesce(SUM(s.ocean_outbound),0)
- + coalesce(SUM(s.air_outbound_shipments),0)
- + coalesce(SUM(s.air_outbound_perishable_shipments),0)
- THEN
- /* choose the larger of EA-OI (sea) vs EA-AI (air) */
- CASE
- /* ── EA-OI wins ─────────────────────────────────────────────── */
- WHEN coalesce(SUM(s.east_asia_sea),0) >= coalesce(SUM(s.east_asia_air),0)
- THEN
- CASE
- WHEN ROUND(
- SUMIf(s.east_asia_sea , lower(s.incoterm) = 'fob')
- / NULLIF(SUM(s.overall),0) * 100 , 2 ) >= 15
- THEN 'EA_OI_FOB' ELSE 'EA_OI_CIF' END
- /* ── EA-AI wins ─────────────────────────────────────────────── */
- ELSE
- CASE
- WHEN ROUND(
- SUMIf(s.east_asia_air , lower(s.incoterm) = 'fob')
- / NULLIF(SUM(s.overall),0) * 100 , 2 ) >= 15
- THEN 'EA_AI_FOB' ELSE 'EA_AI_CIF' END
- END
- /* ---------- 2. OUTBOUND volume dominates ------------------------------------------- */
- ELSE
- /* identify which outbound lane is really the biggest (ties ≈ first match) */
- CASE
- WHEN coalesce(SUM(s.ocean_outbound),0) >= greatest(
- coalesce(SUM(s.air_outbound_perishable_shipments),0),
- coalesce(SUM(s.air_outbound_shipments),0) )
- THEN
- CASE
- WHEN ROUND(
- SUMIf(s.ocean_outbound , lower(s.incoterm) IN ('cif','cf','ci'))
- / NULLIF(SUM(s.overall),0) * 100 , 2 ) >= 15
- THEN 'OO_CIF' ELSE 'OO_FOB' END
- WHEN coalesce(SUM(s.air_outbound_perishable_shipments),0)
- >= coalesce(SUM(s.air_outbound_shipments),0)
- THEN
- CASE
- WHEN ROUND(
- SUMIf(s.air_outbound_perishable_shipments ,
- lower(s.incoterm) IN ('cif','cf','ci'))
- / NULLIF(SUM(s.overall),0) * 100 , 2 ) >= 10
- THEN 'AO_P_CIF' ELSE 'AO_P_FOB' END
- /* default: regular AO is biggest -------------------------------- */
- ELSE
- CASE
- WHEN ROUND(
- SUMIf(s.air_outbound_shipments ,
- lower(s.incoterm) IN ('cif','cf','ci'))
- / NULLIF(SUM(s.overall),0) * 100 , 2 ) >= 15
- THEN 'AO_CIF' ELSE 'AO_FOB' END
- END
- END AS trade_category,
- SUM(s.cn_in) AS cn_in_sum,
- SUM(s.sea_in) AS sea_in_sum,
- SUM(s.in_us) AS in_us_sum,
- SUM(s.in_gulf) AS in_gulf_sum,
- SUM(s.in_eafrica) AS in_eafrica_sum,
- SUM(s.in_wafrica) AS in_wafrica_sum,
- SUM(s.in_europe) AS in_europe_sum,
- CASE
- WHEN SUM(s.cn_in) >= 480 THEN 'Super Large'
- WHEN SUM(s.cn_in) >= 120 THEN 'Large'
- WHEN SUM(s.cn_in) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.cn_in) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.cn_in) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "China-India",
- -- SEA-India
- CASE
- WHEN SUM(s.sea_in) >= 480 THEN 'Super Large'
- WHEN SUM(s.sea_in) >= 120 THEN 'Large'
- WHEN SUM(s.sea_in) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.sea_in) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.sea_in) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "SEA-India",
- -- India-US
- CASE
- WHEN SUM(s.in_us) >= 480 THEN 'Super Large'
- WHEN SUM(s.in_us) >= 120 THEN 'Large'
- WHEN SUM(s.in_us) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.in_us) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.in_us) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "India-US",
- -- India-Gulf
- CASE
- WHEN SUM(s.in_gulf) >= 480 THEN 'Super Large'
- WHEN SUM(s.in_gulf) >= 120 THEN 'Large'
- WHEN SUM(s.in_gulf) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.in_gulf) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.in_gulf) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "India-Gulf",
- -- India-EAfrica
- CASE
- WHEN SUM(s.in_eafrica) >= 480 THEN 'Super Large'
- WHEN SUM(s.in_eafrica) >= 120 THEN 'Large'
- WHEN SUM(s.in_eafrica) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.in_eafrica) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.in_eafrica) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "India-EAfrica",
- -- India-WAfrica
- CASE
- WHEN SUM(s.in_wafrica) >= 480 THEN 'Super Large'
- WHEN SUM(s.in_wafrica) >= 120 THEN 'Large'
- WHEN SUM(s.in_wafrica) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.in_wafrica) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.in_wafrica) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "India-WAfrica",
- -- India-Europe
- CASE
- WHEN SUM(s.in_europe) >= 480 THEN 'Super Large'
- WHEN SUM(s.in_europe) >= 120 THEN 'Large'
- WHEN SUM(s.in_europe) BETWEEN 48 AND 119 THEN 'Medium'
- WHEN SUM(s.in_europe) BETWEEN 9 AND 47 THEN 'Small'
- WHEN SUM(s.in_europe) BETWEEN 1 AND 8 THEN 'Micro'
- ELSE NULL
- END AS "India-Europe",
- arrayFilter(x -> x IS NOT NULL, [SUM(s.cn_in), SUM(s.sea_in), SUM(s.in_us), SUM(s.in_gulf),
- SUM(s.in_eafrica), SUM(s.in_wafrica), SUM(s.in_europe)]) AS lane_totals,
- -- Determine the lane position based on the maximum value in lane_totals
- arrayMax(lane_totals) AS max_lane_value,
- -- Assign sub_cohort based on the maximum lane value
- CASE
- WHEN arrayMax(lane_totals) = SUM(s.cn_in) THEN 'China-India'
- WHEN arrayMax(lane_totals) = SUM(s.sea_in) THEN 'SEA-India'
- WHEN arrayMax(lane_totals) = SUM(s.in_us) THEN 'India-US'
- WHEN arrayMax(lane_totals) = SUM(s.in_gulf) THEN 'India-Gulf'
- WHEN arrayMax(lane_totals) = SUM(s.in_eafrica) THEN 'India-EAfrica'
- WHEN arrayMax(lane_totals) = SUM(s.in_wafrica) THEN 'India-WAfrica'
- WHEN arrayMax(lane_totals) = SUM(s.in_europe) THEN 'India-Europe'
- END AS sub_cohort
- FROM orgs org
- LEFT JOIN shipments s ON s.lead_id = org.lead_organization_id OR s.registration_number = org.registration_number
- GROUP BY org.organization_id
- ),
- rfm_raw AS (
- SELECT
- lead_id,registration_number,
- groupArrayDistinct(shipment_year) AS years_present,
- CASE toUInt32(max(shipment_year))
- WHEN 2025 THEN 5
- WHEN 2024 THEN 4
- WHEN 2023 THEN 3
- WHEN 2022 THEN 2
- END AS recency_score
- FROM kavach.lead_shipment_commodity_stats_v2 final
- WHERE shipment_year IN (2022,2023,2024,2025)
- GROUP BY lead_id, registration_number
- ),
- rfm_cleaned AS
- (
- SELECT
- organization_id,
- MAX(recency) AS max_recency,
- MAX(frequency) AS max_frequency
- FROM (
- SELECT
- DISTINCT organization_id,
- MAX(recency_score) AS recency,
- multiIf(
- arraySort(r.years_present) = [2022, 2023, 2024, 2025], 5,
- arraySort(r.years_present) = [2023, 2024, 2025], 4,
- arraySort(r.years_present) = [2024, 2025], 3,
- arraySort(r.years_present) = [2022, 2023, 2024], 2,
- 1
- ) AS frequency
- FROM rfm_raw r
- LEFT JOIN orgs o
- ON r.lead_id = o.lead_organization_id
- OR r.registration_number = o.registration_number
- GROUP BY organization_id, r.years_present
- ) subquery
- GROUP BY organization_id
- )
- SELECT
- o.lead_organization_id,
- o.organization_id, business_name,
- registration_number,
- ind_contact,
- cse,
- shipment_status,
- enquiry_status,
- overall_strength,
- overall_strength_ea_oi AS "EA_OI",
- overall_strength_ea_ai AS "EA_AI",
- overall_strength_oo AS "OO",
- overall_strength_ao AS "AO",
- overall_strength_ao_p AS "AO_P",
- trade_category,
- sub_cohort AS trade_lane,
- "China-India",
- "SEA-India",
- "India-Gulf",
- "India-Europe",
- "India-EAfrica",
- "India-WAfrica",
- "India-US",
- CASE
- WHEN max_recency IS NULL THEN 1
- ELSE max_recency
- END AS recency,
- CASE WHEN
- max_frequency = 0 THEN 1
- ELSE max_frequency
- END AS frequency,
- CASE
- WHEN ihls_overall_strength = 'Super Large' THEN 5
- WHEN ihls_overall_strength = 'Large' THEN 4
- WHEN ihls_overall_strength = 'Medium' THEN 3
- WHEN ihls_overall_strength = 'Small' THEN 2
- WHEN ihls_overall_strength = 'Micro' THEN 1
- ELSE 1
- END AS monetary
- FROM orgs o
- LEFT JOIN stakeholders ss on o.organization_id = ss.organization_id
- LEFT JOIN aggregations s on o.organization_id = s.organization_id
- LEFT JOIN rfm_cleaned AS r ON r.organization_id = o.organization_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement