SELECT
P.EmployeeId,
(P.Amount*1)*T.RegularHours as RegularEarnings
,(P.Amount*1.5)*T.OTHours as OvertimeEarnings
,(P.Amount*2)*T.DOTHours as DoubleOvertimeEarnings
,((P.Amount*1)*T.RegularHours)+((P.Amount*1.5)*T.OTHours )+((P.Amount*2)*T.DOTHours)+((SELECT ISNULL(SUM(AMOUNT),0) FROM EmployeeEarning WHERE EmployeeId=T.EmployeeId)) as GrossPay
FROM TimeSheet as T
INNER JOIN PayInformation as P ON T.EmployeeId=P.EmployeeId
上面是我的select语句,它以列格式返回收入。我想unpivot
我的结果表,以便每个employeeId
有两列,一列是赚取类型,第二列是赚取金额。
我已经完成了简单的univote
select unpvt.car_id, unpvt.attribute, unpvt.value
from @cars c
unpivot (
value
for attribute in (Make, Model, Color)
) unpvt
但现在卡住了,因为我必须取消选择在select语句中计算的表值。
我尝试了更多内容,这是我的最新代码
SELECT unpvt.EmployeeId, unpvt.Earning, unpvt.Amount
FROM
(SELECT
P.EmployeeId,
(P.Amount*1)*T.RegularHours as RegularEarnings
,(P.Amount*1.5)*T.OTHours as OvertimeEarnings
,(P.Amount*2)*T.DOTHours as DoubleOvertimeEarnings
,((P.Amount*1)*T.RegularHours)+((P.Amount*1.5)*T.OTHours )+((P.Amount*2)*T.DOTHours)+((SELECT ISNULL(SUM(AMOUNT),0) FROM EmployeeEarning WHERE EmployeeId=T.EmployeeId)) as GrossPay
FROM TimeSheet as T
INNER JOIN PayInformation as P ON T.EmployeeId=P.EmployeeId )as c
unpivot (
Amount
for Earning in (RegularEarnings,OvertimeEarnings,DoubleOvertimeEarnings)
)unpvt
现在我收到错误
The type of column "OvertimeEarnings" conflicts with the type of other columns specified in the UNPIVOT list.