似乎无法找到格式足够接近以获得可靠答案的格式。试图弄乱选择案例陈述,但事实证明这是一种痛苦。我有一个基本的工资单数据库我正在设置(如下图所示),我的想法是我想做以下声明如果员工的员工类别(EmpCat)是1,但如果是2则是单独的声明(基本上每小时vs工资)。对于摆弄T SQL仍然相当新,所以任何帮助都将非常感激。
编辑:我已经调整了程序。它看起来应该做我现在想做的事情,甚至应该自动加总工资期间的工作总时数,但我现在收到一个错误,说明它没有包含在聚合函数或GROUP BY子句中。无法确定这是一个问题。CREATE PROCEDURE NewTimesheet(
@LName varchar(50),
@FName varchar(50),
@ShiftType char(2),
@StartDate date,
@EndDate date,
@OTHours decimal(18,2),
@HolidayHours decimal(18,2)
)
AS
BEGIN
INSERT INTO tblTimesheet(
EmpID,
ShiftType,
StartDate,
EndDate,
TotalHours,
OTHours,
HolidayHours,
NetPay
)
SELECT e.EmpID, @ShiftType, @StartDate, @EndDate, SUM(HoursWorked) ,
@OTHours, @HolidayHours,
CASE WHEN ed.EmpCat = 1
THEN (SUM(HoursWorked) * PayRate)
ELSE (e.PayRate / 24)
END
FROM tblEmployee e INNER JOIN tblEmployeeDetail ed ON e.EmpID = ed.EmpID
inner join tblShifts on e.EmpID = tblShifts.EmpID
WHERE LName=@LName AND FName=@FName AND Date > @StartDate AND Date <
@EndDate
GROUP BY e.EmpID, ed.EmpCat, e.PayRate, tblShifts.HoursWorked
END
答案 0 :(得分:1)
如果你想要一个sp来处理两者,你需要在StoredProc中使用SELECT语句,如下所示:
SELECT e.EmpID, @ShiftType, @StartDate, @EndDate, @TotalHours, @OTHours,
@HolidayHours, CASE WHEN ed.EmpCat = 1 THEN (@TotalHours *
tblEmployee.PayRate) ELSE (insert logic for salary) END
FROM tblEmployee e INNER JOIN tblEmployeeDetail ed ON e.EmpID = ed.EmpID
WHERE LName=@LName AND FName=@FName
否则,如果您只想处理每小时的作品,那么您可以使用:
SELECT e.EmpID, @ShiftType, @StartDate, @EndDate, @TotalHours, @OTHours,
@HolidayHours, @TotalHours * tblEmployee.PayRate
FROM tblEmployee e INNER JOIN tblEmployeeDetail ed ON e.EmpID = ed.EmpID
WHERE LName=@LName AND FName=@FName AND ed.EmpCat = 1
HTH