Calculating the holidays of employees without counting the weekends - sql

I'm using Microsoft Access to calculate the number of holidays of doctors. Doctors have up to 31 days for holidays per year from where I extract the count of holidays.
However I don't want to count the weekends between start and end days.
Currently my code is the following, which counts the weekends:
TRANSFORM 31-Nz(Sum(DateDiff("d",DateAdd("d",-1,leaves.leave_starting_date),leaves.leave_end_date)),0) AS Days
SELECT doctors.Name
FROM doctors LEFT JOIN leaves ON doctors.ID = leaves.doctor_id
GROUP BY doctors.Name
PIVOT Year(leaves.leave_starting_date);
Any help?

Replace DateDiff with a function that excludes weekends (and, optionally, public holidays as well) from the difference in calendar days.
An example is my function DateDiffWorkdays posted here.

Related

How can I SUM a field based on a different date range?

I have two main tables; calendar and transactions. I'm trying to SUM the transactions for each Year, Term, and Week for each store in separate queries. However, I'm also trying to return the previous year's Sum for each Year/Term/Week, but the calendar we use is an accounting calendar that shifts dates YoY.
My question is, how do I return the SUM for the previous Year's period of aggregation in the same line?
To put in another way - I am trying to take window periods of dates, and then sum the transactions for each window period, but the window periods are for previous periods of aggregation and then get those to return in the same line.

SQL calculate number of days in month excluding weekends and holidays days

I have approximately the same table (excluding count column). I want to calculate the number of working days (Mon-Fri) and exclude public holidays.
I tried to try the following query
SELECT count(distinct(date)) from MYDB where dummy <> 1
However, it gives the only total number of days including weekends. Additionally, if use this command it counts distinct dates, however, my dates do not show a full month, so another logic should've used. Could you help to figure out which code is better to use?
there should be a function in Vertica that extracts weekday from date, so to exclude weekends you'll need to add another condition like
extract(dow from date) not in (6,0)
(6 is Sat, 0 is Sun in this case)

SQL count query - how to get desired results?

I have a factory that contains 4 production lines, work in a year which is devised into periods of year. I also have a working days and holidays table that contains these columns:
factory_id, mainline_id, year_id, period_id, holiday check
Holiday check is a boolean column; it's true when the day is holiday, false when it's working day
I want to count all working days in a period of the year that belongs to specific mainline which is belong to specific factory
I tried
select count(holiday_check)
from myTable
where holiday_check = false
but it returns all working days in all periods and all years for all factories
Use the GROUP BY clause:
SELECT COUNT(*) FROM your_table
WHERE holiday_check = false
GROUP BY period_id, factory_id, mainline_id

sql to find the weekdays in the month from the current day

please help me with this. using SQL server 2008
I need to find the number of sales done on the current day.
then find the weekday from current date and based on that find the average of the sales on all those particular weekdays in the last month
ex:
select count(sales) from salestable where orderdate= getdate()
where it gives the count of the sales done on the current date
then I need to find out the average of the sales done on the same weekday for ex if today is Sunday find the average of the sales done in the last month on all Sundays in that month.
I recommend that you borrow the data warehousing technique of creating a Calendar table that you pre-populate with 1 row for every date within the range you might need. You can add to it basically any column that is useful - in this case DayOfWeek and MonthID. Then you can eliminate date math entirely and use joins - sort of like this (not complete but points you in the right direction):
select count(salestable.sales) as salescount, a.salesavg
from salestable
join calendar on salestable.orderdate = calendar.calendardate
join (
select monthid, dayofweek, avg(salestable.sales) as salesavg
from salestable
join calendar on salestable.orderdate = calendar.calendardate
group by monthid, dayofweek) as a
on calendar.monthid = a.monthid and calendar.dayofweek = a.dayofweek
where calendar.calendardate = getdate()
You create and populate the calendar table once and reuse it every time you need to do date operations. Once you get used to this technique, you will NEVER go back to date math.
For this kind of queries are Common Table Expressions very usefull. Then you can use DATEPART function to get day of week.
This solution is also untested and intended to just point you in the right direction.
This solution uses a co-related sub-query to get the average sales.
select
order_date,
count(sales) total_sales,
(select avg(sales)
from sales_table
where order_date between dateadd(day,-30,#your_date) and #your_date
and datepart(WEEKDAY,order_date) = datepart(WEEKDAY,#your_date)
) avg_sales_mth
from sales_table
where order_date = #your_date

calculate days excluding weekends and public holidays in sharepoint2010

How do I exclude weekends(sat and sun) and public holidays when counting number of days between a start and end time in sharepoint 2010 calculate column.
You can exclude weekends like this in the calculated column:
=IF(AND((WEEKDAY(EndDate,2))<(WEEKDAY(StartDate,2)),((WEEKDAY(StartDate,2))-(WEEKDAY(EndDate,2)))>1),(((DATEDIF(StartDate,EndDate,"D")+1))-(FLOOR((DATEDIF(StartDate,EndDate,"D")+1)/7,1)*2)-2),(((DATEDIF(StartDate,EndDate,"D")+1))-(FLOOR((DATEDIF(StartDate,EndDate,"D")+1)/7,1)*2)))
I don't know how about public holidays :)