0

我有一个如下表:

支票 | 账单总额 | 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

非常感谢。

4

1 回答 1

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
于 2012-11-06T10:59:41.897 回答