我有一张如下表:
检查|比尔总计| D1 | D2 | D3 | D4 | D5 |
1 100 10 2 2 50 10 5 7
D1 - D5代表不同的折扣类型,我该如何进行以下操作?
检查|比尔总计|折扣类型|折扣|
1 100 D1 10 1 100 D3 2 2 100 D1 10 2 100 D4 5 2 100 D5 7
非常感谢。
答案 0 :(得分:0)
请查看unpivot的示例:
CREATE TABLE #pvt (VendorID int, Emp1 int, Emp2 int, Emp3 int, Emp4 int, Emp5 int);
INSERT INTO #pvt VALUES (1,4,3,5,4,4);
INSERT INTO #pvt VALUES (2,4,1,5,5,5);
INSERT INTO #pvt VALUES (3,4,3,5,4,4);
INSERT INTO #pvt VALUES (4,4,2,5,5,4);
INSERT INTO #pvt VALUES (5,5,1,5,5,5);
select * from #pvt
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM #pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt;
drop table #pvt