
--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';


