From a7b200dc26853b359b8b8fe397a0c603399b586e Mon Sep 17 00:00:00 2001 From: dipanshuk Date: Tue, 26 May 2026 14:56:02 +0530 Subject: [PATCH] Calculation for clickhouse data --- ClickHouse_Calculated_Query/OSA %.sql | 29 +++++ ClickHouse_Calculated_Query/Promo%.sql | 14 +++ .../Promotion_Table_Description.sql | 106 ++++++++++++++++++ ClickHouse_Calculated_Query/SOSTarget %.sql | 16 +++ ClickHouse_Calculated_Query/gg.sql | 1 - 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 ClickHouse_Calculated_Query/OSA %.sql create mode 100644 ClickHouse_Calculated_Query/Promo%.sql create mode 100644 ClickHouse_Calculated_Query/Promotion_Table_Description.sql create mode 100644 ClickHouse_Calculated_Query/SOSTarget %.sql delete mode 100644 ClickHouse_Calculated_Query/gg.sql diff --git a/ClickHouse_Calculated_Query/OSA %.sql b/ClickHouse_Calculated_Query/OSA %.sql new file mode 100644 index 0000000..8168e1e --- /dev/null +++ b/ClickHouse_Calculated_Query/OSA %.sql @@ -0,0 +1,29 @@ + +--I want to calculate OSA% WHERE use column skuavailability +--:- OSA% calculation = +--1. apply filter on MSL where msl=1 +--2. calculate Y+N = count all the records of skuavailability +--3. calculate Y = count of all the Y in skuavailability +--4. OSA% calculation = (Y/ Y+N)*100 + + + +-- For Numinator +SELECT * FROM "DaburIndia_BI"."Stock_Details" WHERE "MSL" = '1' AND "skuavailability" = 'Y' and +"VisitDate" between '2026-04-01' and '2026-04-30'; + +-- For Demunitor + +SELECT * FROM "DaburIndia_BI"."Stock_Details" WHERE "MSL" = '1' AND skuavailability IN ('Y','N') +"VisitDate" between '2026-04-01' and '2026-04-30'; + +-- OSA % FOR MonthWise + +SELECT SUM(CASE WHEN MSL = '1' AND skuavailability IN ('Y','N') THEN 1 ELSE 0 END) AS Total_Y_N, +SUM(CASE WHEN MSL = '1' AND skuavailability = 'Y' THEN 1 ELSE 0 END) AS Total_Y, +ROUND(SUM(CASE WHEN MSL = '1' AND skuavailability = 'Y' THEN 1 ELSE 0 END) * 100.0/NULLIF(SUM(CASE +WHEN MSL = '1' AND skuavailability IN ('Y','N') THEN 1 ELSE 0 END),0),2) AS OSA_Percentage +FROM DaburIndia_BI.Stock_Details +where "VisitDate" between '2026-04-01' and '2026-04-01'; + + diff --git a/ClickHouse_Calculated_Query/Promo%.sql b/ClickHouse_Calculated_Query/Promo%.sql new file mode 100644 index 0000000..fab79c6 --- /dev/null +++ b/ClickHouse_Calculated_Query/Promo%.sql @@ -0,0 +1,14 @@ + +SELECT + store_id, + SUM(IF( msl = '1' AND present IN ('Y', 'N'), 1, 0)) AS Total_Y_N, + SUM(IF(msl = '1' AND present = 'Y', 1, 0)) AS Total_Y, + ROUND( + (SUM(IF(msl = '1' AND present = 'Y', 1, 0)) * 100.0) / + NULLIF(SUM(IF(msl = '1' AND present IN ('Y', 'N'), 1, 0)), 0),2 + ) AS Promo_Percentage +FROM DaburIndia_BI.Promotion +--WHERE visit_date >= '2026-04-01' +GROUP BY store_id +HAVING SUM(IF(msl = '1' AND present IN ('Y', 'N'), 1, 0)) > 0 +ORDER BY store_id; diff --git a/ClickHouse_Calculated_Query/Promotion_Table_Description.sql b/ClickHouse_Calculated_Query/Promotion_Table_Description.sql new file mode 100644 index 0000000..e06f54f --- /dev/null +++ b/ClickHouse_Calculated_Query/Promotion_Table_Description.sql @@ -0,0 +1,106 @@ + +ALTER TABLE DaburIndia_BI.Promotion +MODIFY COLUMN MID Nullable(Int64) +COMMENT 'Unique master transaction ID (depends on unique combination of employee_id, store_id, and visit_date)'; + +ALTER TABLE DaburIndia_BI.Promotion +MODIFY COLUMN pk Int64 +COMMENT 'Primary key identifier'; + +ALTER TABLE DaburIndia_BI.Promotion +MODIFY COLUMN store_id Int32 +COMMENT 'Unique store identifier'; + +ALTER TABLE DaburIndia_BI.Promotion +MODIFY COLUMN employee_id Int32 +COMMENT 'Employee unique identifier'; + + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN supervisor_id Int32 +COMMENT 'Supervisor unique identifier'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN channel_id Int32 +COMMENT 'Store channel identifier'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN chain_id Int32 +COMMENT 'Store chain identifier'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN storetype_id Int32 +COMMENT 'Store type identifier'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promo_definition_id Nullable(Int32) +COMMENT 'Promotion definition identifier'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promo_definition_name Nullable(Int32) +COMMENT 'Promotion definition identifier'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promo_definition_name Nullable(String) +COMMENT 'Promotion scheme or offer name'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promotion_deatils_id Nullable(Int32) +COMMENT 'Promotion detail unique identifier depends on the promotion_details table'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promotion_deatils Nullable(String) +COMMENT 'Promotion detail name of the table/subcategory '; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promotion_value_id Nullable(Int32) +COMMENT 'Promotion offer value/SubCategory Id'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promotion_value_name Nullable(String) +COMMENT 'Live promotion value name/SubCategory name '; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN promotion_type Nullable(String) +COMMENT 'Type of promotion'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN present Nullable(String) +COMMENT 'Promotion availability status (Y/N)'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN reason Nullable(String) +COMMENT 'Reason for non-availability'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN PromoQuestion Nullable(String) +COMMENT 'Promotion related questionnaire'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN image1 Nullable(String) +COMMENT 'First promotion image path'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN image2 Nullable(String) +COMMENT 'Second promotion image path'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN update_date DateTime64(3) +COMMENT 'Record update date'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN update_by Nullable(String) +COMMENT 'User who updated record'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN ReasonId Nullable(Int32) +COMMENT 'Reason Id for non-availability'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN msl Nullable(Int32) +COMMENT 'Must Stock List indicator 0=promotion Not exist , 1=promotion exist'; + +ALTER TABLE DaburIndia_BI_New.Promotion +MODIFY COLUMN stock Nullable(String) +COMMENT 'Stock availability status'; + diff --git a/ClickHouse_Calculated_Query/SOSTarget %.sql b/ClickHouse_Calculated_Query/SOSTarget %.sql new file mode 100644 index 0000000..0b2aee6 --- /dev/null +++ b/ClickHouse_Calculated_Query/SOSTarget %.sql @@ -0,0 +1,16 @@ +-- SOS Taget Achievement + +SELECT store_id,SOSHeaderName,ChildSelfFacing,SOSHeaderFacing,IFNULL(Avg_SOSTarget, 0) AS Avg_SOSTarget, +ROUND(ChildSelfFacing * 1.0 / NULLIF(SOSHeaderFacing, 0),6) AS SOS, +IF((ChildSelfFacing * 1.0 /NULLIF(SOSHeaderFacing, 0)) >= (IFNULL(Avg_SOSTarget,0) / 100.0), +1,0) AS SOSCompliance, +LEAST(100.0, +ROUND(((ChildSelfFacing * 1.0 /NULLIF(SOSHeaderFacing, 0))/NULLIF(IFNULL(Avg_SOSTarget,0) / 100.0, 0)) * 100,2)) AS SOSAchievement_Percentage +FROM +( +SELECT store_id,SOSHeaderName, +SUM(ChildSelfFacing) AS ChildSelfFacing,SUM(SOSHeaderFacing) AS SOSHeaderFacing, +IFNULL(ROUND(AVG(SOSTarget), 2),0) AS Avg_SOSTarget FROM DaburIndia_BI.SOS_OneApp +GROUP BY store_id, SOSHeaderName) t +ORDER BY store_id,SOSHeaderName; + diff --git a/ClickHouse_Calculated_Query/gg.sql b/ClickHouse_Calculated_Query/gg.sql deleted file mode 100644 index 5c979ee..0000000 --- a/ClickHouse_Calculated_Query/gg.sql +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file