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
以上是我的选择语句,它以列格式返回我的收入。我希望unpivot
我的结果表employeeId
有两列,一列是收入类型,第二列是收入金额。
我已经完成了简单的 unpivote
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.