[MSSQL] UNPIVOT

SQL 2014. 3. 7. 11:26

 



--Create the table and insert values as portrayed in the previous example. CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,     Emp3 int, Emp4 int, Emp5 int); GO 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); GO --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; GO

 

------------------------------------------------------------

 

 

'SQL' 카테고리의 다른 글

[MSSQL] with(nolock)  (0) 2014.04.30
[MSSQL] 테이블 컬럼 및 프로시저 특정단어 검색  (0) 2014.04.21
[MSSQL] 공백 제거  (0) 2014.02.10
[MSSQL] SQL Server Cache (캐쉬) / Buffer (버퍼)  (0) 2013.12.23
[MSSQL] 테이블의 종류  (0) 2013.12.17
Posted by 요지
,