# Calculate AVG by type of product and group by month

Copper Contributor

# Calculate AVG by type of product and group by month

Hi all,

Any help how can i get the result on below table based on image attaches?

The pretented result:

Thank you

4 Replies

# Re: Calculate AVG by type of product and group by month

hi,
you can use this code

SELECT Month,
MAX(CASE WHEN ProductType='FL' THEN avg END) "AVG LeadTimeOFFL",
MAX(CASE WHEN ProductType='PH' THEN avg END) "AVG LeadTimeoFPH"
FROM (
SELECT MONTH(DATE) Month,ProductType,AVG(LeadTime) avg FROM ProductTable GROUP BY MONTH(DATE),ProductType
) as fdfdf
GROUP BY Month;

# Re: Calculate AVG by type of product and group by month

Month 2: So year 2024 vs 2025 don't matter for the result?

# Re: Calculate AVG by type of product and group by month

don't matter for the result.it consider on the basis of months

# Re: Calculate AVG by type of product and group by month

This should work for you:

``````SELECT MONTH(T.Date) AS Month,
AVG(CASE WHEN T.ProductType = 'FL' THEN T.LeadTime ELSE NULL END) AS LeadTimeOfTypeFL,
AVG(CASE WHEN T.ProductType = 'PH' THEN T.LeadTime ELSE NULL END) AS LeadTimeOfTypePH
FROM yourTable AS T
GROUP BY MONTH(T.Date)``````