There are three fields: start date, end date and holiday date. I want to count business days between start date and end date. Of course I want to do this excluding holidays. I flagged the holidays by creating a calendar table. As a result, I have three fields and I don't know how to calculate the workday count with these data. I'm waiting for your help. thank you.
Related
I need to report the average number of customer visits each engineer makes per month.
My SQL query creates a temporary table of months between the start date and end date and left joins this to the main data table, to ensure rows are returned even for months where no visits were made.
So an example of the data returned from SQL may be:
My report has two column groups, one for year and one for month, and I have a row group for the engineer.
For this report, the date is always returned as the first of the month, even though the actual visit could be on any date.
At the end of each year there is a cell which contains Count(Customer) and totals the number of visits the engineer made in that year. I would also like to have a cell which displays the average number of visits made each month in that year.
For a complete year I could simply divide by 12. However for a partial year I need to count the number of month columns for that year.
I tried CountDistinct(Month) but this only counts months where at least one visit was made, making the monthly average incorrect.
How can I get a count of the number of columns in a column group, including columns with no data?
Thanks.
The way I would do this would be to add a column into your temporary dates table that had the number of months selected in it.
You could do this by either counting the months in the temp table then appending the value to it or, if the dates table contains more than just months then work it out based on the parameters you pass in.
For example
SELECT *, DATEFIFF("m", #startDate, #endDate) as NoOfMonths
INTO #myTempDateTable
FROM myDateTable
WHERE etc...
Then in SSRS you can simply divide your total by this number.
i have a table for offers that contains 2 columns from_date and to_date and the values 2014-09-15 and 2014-10-31.
Then i have a user that wants to book from 2014-09-10 til 2014-09-25, what i would like to do is to count how many days from the user dates matches the from_date and to_date on the offers table.
can anyone advise on how to make this count
thank you
One way would be to find the difference between the latest start date and the earliest end date:
SELECT DATEDIFF (GREATEST(from_date, '2014-09-10'),
LEAST(to_date, '2014-09-25')
)
FROM mytable
I've got a date column that represents a user's birthday, but the year is not used (to avoid age data, which can be sensitive for some users :)). I need to compare it to another column to see if the user has completed training since the last occurrence of the last day of their birth month.
In other words, if the training_date occurs after the last day of the month of the last occurrence of their birth month in the past. For this question, assume the birthday column is called birthday.
where `training`.`date_training` BETWEEN MAKEDATE(Year(CURDATE()) -1,dayofyear(`user`.`birthdate`)) AND MAKEDATE(Year(CURDATE()) -1,dayofyear(`user`.`birthdate`));
Edit: birthday is stored with an arbitrary year, currently the informal business rule is use the year the record was entered.
if year is considered
(YEAR(training_date) > YEAR(birthday)) || ((YEAR(training_date) = YEAR(birthday)) && (MONTH(training_date) > MONTH(birthday)))`
else if month only,
MONTH(training_date) > MONTH(birthday)
where I come from:
I have the tables 'event', user and user_event. Table event stores datetimes for start and end of an event. User_event holds information in which event a user is attending. Now it is easy possible to calculate the SUM of time a certain user is scheduled in events by just adding up all event durations.
But I need something different. Assuming that it is monday and user A is attending an event from 9-10am and from 2-3pm. The sum of these events is 2hrs. But I want to calulate how long the presence of a user is to take part in these events. In my example this is 6hr (from 9am - 3pm). I achieved this for one day (with time difference of max(event) and min(event) for the user and a given day.
Where I wanna go:
But I canĀ“t find a solution for the task to sum the presence for the whole week. I need to add up all presence-sums for each day. But I cannot use the solution given for one day because min() and max() will only give me unique min and max values for the whole week and not for each day of the week.
Hope I described it well enough.
Thanks!
Make a GROUP BY on the days first, then on the week:
select
sum(d.dayFinish - d.dayStart) as weeklyPresence,
week(d.day) as weekNumber
d.userId
from (
select
to_date(e.startDate) as day,
min(e.startDate) as dayStart
max(e.finishDate) as dayFinish,
e.userId
from events e
group by to_date(e.startDate), e.userId
) d
group by week(d.day), d.userId
(I did not follow the actual syntax, some type conversions may be needed).
I suppose you have an Events table with startDate and endDate columns, and a userId foreign key that refers to the user.
The to_date() function rounds to the day (we group on that first), and the week() function determines the week (that's our second grouping).
I have a staff timesheet table where i have timestamp of when those records are created. I now want to generate the report so that my start date is Tuesday and end date is next Monday, which is 1 week. Now i need to generate all the records grouped by this weeks time but will be next set of tuesday to monday.
This is like normal GROUP BY WEEK(Timestamp) but the WEEK numbers are not the default ones i need to generate the reports in this custom duration. I have a query working for this which groups the record efficiently by Week 1, week 2, week 3 etc.. which is picked from default mysql calendar i guess. How can i change that to generate reports grouped by custom weeks ?
Can you tel me how the following works as how the dates are picked up ?
SELECT WEEK(pw.date) AS Date,DATE_FORMAT(pw.date,'%d-%m-%Y') AS post_date,
SUM(wages) AS amount,SUM(pw.hours) AS hours,SUM(pw.minutes) AS minutes
FROM pos_sessions pw
GROUP BY YEAR(pw.date), WEEK(pw.date) ORDER BY pw.date DESC
I think this Other Solution is what you are looking for...