我看到过一些帖子,可以获得数据透视表的结果,但不能逆转,想知道是否有清洁的方法来实现?如果没有,那么任何解决方法也可以。
在管理工作室中执行此操作以查看逆转结果
CREATE TABLE [dbo].[Payment](
[PaymentId] [int] NOT NULL,
[EmployeeId] [int] NOT NULL,
[RegularHours] [decimal](18, 0) NULL,
[OvertimeHOurs] [decimal](18, 0) NULL
) ON [PRIMARY]
go
insert into payment values (1, 1, 40, 10)
insert into payment values (1, 2, 20, 0)
go
select * from payment
select * from payment unpivot ([hours] for [paytype] in ([RegularHours], [OvertimeHOurs]))a
第一条Select语句的输出
PaymentId EmployeeId RegularHours OvertimeHOurs
----------- ----------- ---------------------------------------
1 1 40 10
1 2 20 0
(2 row(s) affected)
第二个选择语句的输出,这就是我要找的。
PaymentId EmployeeId hours paytype
----------- ----------- -----------------------------------------------------
1 1 40 RegularHours
1 1 10 OvertimeHOurs
1 2 20 RegularHours
1 2 0 OvertimeHOurs
(4 row(s) affected)
{1 => {RegularHours => 60; OvertimeHours => 10}}
? - Michael Schnerring