我看过一些帖子可以让您获取数据透视结果但不会取消数据透视,需要知道是否有任何干净的方法可以实现?如果不是任何解决方法也可以吗?
执行此操作以在 Management Studio 中查看 unpivot 结果
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)
第二个 Select 语句的输出&这就是我要找的
PaymentId EmployeeId hours paytype
----------- ----------- -----------------------------------------------------
1 1 40 RegularHours
1 1 10 OvertimeHOurs
1 2 20 RegularHours
1 2 0 OvertimeHOurs
(4 row(s) affected)