I’m wracking my brain on this one and I think it is not quite as challenging as I’m making it out to be. Effectively what I am trying to do is:
Calculate the monthly average of sales while excluding the days that are holiday type 1 which apply to all regions (I can change this to Type 2 for a specific region, for example).
SELECT Year(Date) AS Year, Month(Date) AS Month, Avg(tblSales.Sales) AS AvgSales
FROM tblSales
LEFT JOIN tblHolidays
ON tblSales.[Date] >= tblHolidays.[Date From] AND tblSales.[Date] <= tblHolidays.[Date To]
WHERE tblHolidays.[Date From] Is Null AND tblHolidays.[Type]<> 1 AND tblHolidays.[Region]<>"All"
GROUP BY Year(Date), Month(Date);
Right now I’m not sure that I understand whether or not this is working, while it does produce results. What I’m not seeing is where it is telling tblHolidays not to use Type 2 and Type 3 holidays, or holidays that apply to specific regions when comparing to tblSales.[Date].
Any ideas?
This is really what you mean.
The LEFT JOIN serves to bring in matches from tblHolidays where it matches tblSales on ALL the conditions in the
ONclause. In this case, it’s saying:Because of the LEFT JOIN nature, it will preserve all rows from tblSales even if no matches were found from tblHolidays. Next, where the matches were found, we strike them off by using the filter
tblHolidays.[Date From] Is Null. BecausetblHolidays.[Date From]is not null where the holiday condition was found.