简体   繁体   中英

Am I calculating average income per hour by day of week correctly?

data table looks like this

Use a query to calculate average income per hour by day of week.

SELECT WEEKDAY(date_start_time), SUM(total_income)/SUM(DATEDIFF((hour, 
date_start_time, date_end_time) AS avg_income
FROM Deliveries
GROUP BY WEEKDAY(date_start_time)

Things to know:

  • Entry_id is a unique key for each time the employee comes into the office
  • There will be many records of the same user_id if an employee comes into the office repeatedly
  • Tasks completed will most likely stay unused in this question

Am I appropriately answering this question?

Things I am concerned about:

1) Does DATEDIFF only return an integer value? If thats the case, then to have a better estimation of the avg_income does this mean we should use DATEDIFF(minutes, ..., ...) and then calculate the hours with decimal places from that integer?

2) Are people working overnight shifts something that I need to worry about? How much more complicated would it make this query?

3) Moving onward if I was asked to "calculate the average earnings per hour during 9am to 5pm" does this mean I need to calculate this for each individual employee... or for each individual hour (ie. ultimately am I grouping by hour or by user_ID)?

1) Use timediff()

2) You will not only need to consider overnight shifts but you will need to consider overtime pay if they work > 40 hours in between the week start date and the week end date for a given week. This is only if employees are paid different hourly rates for these (ex.time and a half). If this is a factor then you will need to roll up your sleeves because it will be a full algorithm.

3) This depends on what you are trying to find the average by (user, day, etc.) but a simple way would be to just nest your select and grab an avg().

select avg(earnings) overall_average from 
(select user, [calculated_earnings] as earnings from [table] where [conditions])

select avg(earnings) overall_average from 
(select weekday, [calculated_earnings] as earnings from [table] where [conditions])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM