我正在尝试创建一个数据库,员工可以在这些数据库中进行注册,以及他们在那一天工作的时间。
到每个月末,我应该能够提取每位员工的总小时数,应该支付的总小时数(有不同的适用费率 - 例如,如果一个人工作时间从5:30到7 :00,他们工作了1.5个小时,但他们将获得3个小时的工作时间。)
所以,当我每天尝试这些时间时,我没有任何问题:
idemployee idhours_count date total total_to_pay
1 1 2015-05-21 6.5000 8.350000
1 2 2015-05-22 7.9167 14.083333
1 3 2015-05-23 10.0833 17.645833
3 4 2015-05-26 5.0000 5.000000
3 5 2015-06-06 11.8333 20.850000
3 6 2015-06-06 8.6667 8.750000
为什么2015-05-22为员工1有2个条目?好吧,他可能在白天做了两班倒。 并非我们折磨他做那件事,但不是。
总计在视图 hours_summary2 中计算,其中包含小时的详细信息(班次1小时,班次2小时等),并计算总支付金额在不同的视图中,*,其中也定义了所有费率。
无论如何,为了获得月度结果,我创建了一个最后一个视图,它还提供了我提到的视图中的小时详细信息。代码看起来像这样:
CREATE
ALGORITHM = UNDEFINED
DEFINER = root@localhost
SQL SECURITY DEFINER
VIEW payment_summary AS
SELECT
hours_to_pay.idemployee AS idemployee,
MONTH(hours_to_pay.date) AS month,
YEAR(hours_to_pay.date) AS year,
TRUNCATE(SUM(hours_to_pay.total), 2) AS Number_hours,
TRUNCATE(SUM(hours_to_pay.total_to_pay),
2) AS Number_hours_to_pay,
/* Code for the detail of the hours */
SUM(hours_calc_summary.ew_es) AS early_week_early_start,
SUM(hours_calc_summary.early_week_normal) AS early_week_normal,
SUM(hours_calc_summary.wednesday_overtime) AS wednesday_overtime,
SUM(hours_calc_summary.ew_es_h) AS early_week_early_start_holiday,
SUM(hours_calc_summary.early_week_holiday) AS early_week_whole_holiday,
SUM(hours_calc_summary.wednesday_overtime_h) AS wednesday_overtime_holiday,
SUM(hours_calc_summary.late_week_normal) AS late_week_normal,
SUM(hours_calc_summary.lw_le) AS late_week_late_end,
SUM(hours_calc_summary.late_week_holiday) AS late_week_holiday,
SUM(hours_calc_summary.lw_le_h) AS late_week_late_end_holiday,
SUM(hours_calc_summary.sat_normal) AS sat_normal,
SUM(hours_calc_summary.sat_overtime) AS sat_overtime,
SUM(hours_calc_summary.sat_holiday) AS sat_holiday,
SUM(hours_calc_summary.sat_overtime_h) AS sat_overtime_holiday,
SUM(hours_calc_summary.sun_normal) AS sun_normal,
SUM(hours_calc_summary.sun_overtime) AS sun_overtime,
SUM(hours_calc_summary.sun_holiday) AS sun_holiday,
SUM(hours_calc_summary.sun_overtime_h) AS sun_overtime_h,
SUM(hours_calc_summary.travel_time_1) AS travel_time_1,
SUM(hours_calc_summary.travel_time_2) AS travel_time_2,
SUM(hours_calc_summary.excess_hour_weekday) AS excess_hour_weekday,
SUM(hours_calc_summary.excess_hour_saturday) AS excess_hour_saturday,
SUM(hours_calc_summary.excess_hour_sunday) AS excess_hour_sunday,
SUM(hours_calc_summary.excess_hour_holiday) AS excess_hour_holiday
FROM
(hours_calc_summary
JOIN hours_to_pay)
WHERE
((hours_calc_summary.idemployee = hours_to_pay.idemployee)
AND (hours_calc_summary.date = hours_to_pay.date))
GROUP BY hours_to_pay.idemployee , month , year
(如果我在引号之间放置日期, hours_to_pay.date 调用有效,我为了格式化而删除了它们)
现在,如果我要手动进行计算:
但是,结果如下:
idemployee month year number_hours number_hours_to_pay
1 5 2015 24.50 40.07
3 5 2015 5.00 5.00
3 6 2015 41.00 72.28
6月份的员工3有更多的时间!
在做了一些试验和错误之后,看起来在同一天报告时间会产生一些时髦的结果,因为它们每次计算两次。因此,该视图认为员工3的6月有4个条目,而只有2个条目。
所以我想知道,我的代码出了什么问题?我试图更改GROUP BY条件,但没有雪茄(或者没有任何线索):在我看来,按月分组应该已经足够了,而按天分组不会报告每月的小时数。
还提出更相关标题的建议。