MySQL存储过程计算工资报告

时间:2015-02-15 02:21:25

标签: mysql sql stored-procedures

我在MySQL中使用存储过程来计算工资报告时遇到了一些麻烦。

数据库如下所示

**Employee**
EmployeeID
....
....

**Timesheet**
TimesheetID
EmployeeID
....

**TimesheetDetails**
TimesheetDetailID
TimesheetID
ProjectID
WorkDate
Hours

**EmployeePayRate**
EmployeeID
Rate
StartDate

支付期为两周,问题是支付期间支付率可能会多次改变。

这是我迄今为止的查询存储过程。

CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `sp_report_payroll`(IN p_startDate VARCHAR(15), IN p_endDate VARCHAR(15))
BEGIN

    SELECT
        e.EmployeeID AS EmployeeID,
        CONCAT(e.LastName, ', ', e.FirstName) AS Resource,
        (SELECT rt.Description FROM Roles rt WHERE rt.RoleId = e.RoleId) as Role,
        (
            SELECT
                SUM(td.BillableHours)
            FROM
                Timesheets t,
                TimesheetDetails td,
                Projects p
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND p.ProjectID = td.ProjectID
            AND p.ProjectID NOT IN (24,29,34,606,614,746)
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
        ) AS RegularHours,
        (
            SELECT
                SUM(td.BillableHours)
            FROM
                Timesheets t,
                TimesheetDetails td,
                Projects p
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND p.ProjectID = td.ProjectID
            AND (p.ProjectID = 29 OR p.ProjectID = 614)
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
        ) AS PTO,
        (
            SELECT
                SUM(td.BillableHours)
            FROM
                Timesheets t,
                TimesheetDetails td,
                Projects p
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND p.ProjectID = td.ProjectID
            AND p.ProjectID = 24
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
        ) AS Holiday,
        (
            SELECT
                SUM(td.BillableHours)
            FROM
                Timesheets t,
                TimesheetDetails td,
                Projects p
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND p.ProjectID = td.ProjectID
            AND p.ProjectID = 746
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
        ) AS FloatingHoliday,
        (
            SELECT
                SUM(td.BillableHours)
            FROM
                Timesheets t,
                TimesheetDetails td
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND td.ProjectID NOT IN (34,606)
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
        ) AS Total,
        (
            SELECT 
                epr.Rate 
            FROM 
                EmployeePayRate epr ,
                Timesheets t,
                TimesheetDetails td
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND epr.EmployeeID = t.EmployeeID
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
            AND epr.StartDate <= td.WorkDate
            ORDER BY epr.StartDate DESC
            LIMIT 1
        ) AS PayRate,
        (
            SELECT
            (SELECT 
                epr.Rate 
            FROM 
                EmployeePayRate epr ,
                Timesheets t,
                TimesheetDetails td
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND epr.EmployeeID = t.EmployeeID
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate)
            AND epr.StartDate <= td.WorkDate
            ORDER BY epr.StartDate DESC
            LIMIT 1)

            *

            (SELECT
                SUM(td.BillableHours)
            FROM
                Timesheets t,
                TimesheetDetails td
            WHERE e.EmployeeID = t.EmployeeID
            AND t.TimeCardID = td.TimeCardID
            AND td.ProjectID NOT IN (34,606)
            AND DATE(td.WorkDate) >= DATE(p_startDate)
            AND DATE(td.WorkDate) <= DATE(p_endDate))
        ) AS GrossEarnings
    FROM
        Employee e
    WHERE (e.EmployeeID IN (SELECT t.EmployeeID 
        FROM TimesheetDetails tsd, Timesheets t 
        WHERE tsd.BillableHours > 0 
        AND tsd.WorkDate BETWEEN p_startDate AND p_endDate
        AND tsd.TimeCardID = t.TimeCardID
        ) OR e.Status = 'Active')
    AND (e.ResourceTypeID = 2 OR e.ResourceTypeID = 4)
    GROUP BY Resource
    ORDER BY Role, Resource ASC;

END

然而,这并不能说明在支付期间改变工资率。任何人都可以提供处理这种情况的最佳方法。将一些子查询分解为函数会更清晰吗?

更新:

报告应显示以下列:

雇员 员工姓名(LastName,FirstName) 角色 正常工作时间 PTO时间 假期时间 浮动假期 全部小时数 总收入

根据要求,我提供了数据,但附件对于stackoverflow来说很重要。链接在这里用于mysql转储。我将它放入我的OneDrive的公共文件夹中。

https://onedrive.live.com/?cid=4D42EF88BE1FD96C&id=4D42EF88BE1FD96C%215903

0 个答案:

没有答案