如何将行的某些字段转换为emp_id,month,year列

时间:2016-02-11 11:28:05

标签: sql-server excel

表格数据:

code        salary      TA     DA     month   year 
--------------------------------------------------
01          30000     5000    1000     01    2015 

需要输出:

Code         amount   month  year 
------------------------------------
01             30000      01  2015  
01              5000      01  2015
01              1000      01  2015

Please check the data in table and required output in image format

2 个答案:

答案 0 :(得分:0)

尝试使用Cross Apply with Values, 下面的代码是根据图像

afterComplete

答案 1 :(得分:0)

DECLARE @Table1 TABLE 
    (code int, salary int, TA int, DA int, month int, year int)
;

INSERT INTO @Table1
    (code, salary, TA, DA, month, year)
VALUES
    (01, 30000, 5000, 1000, 01, 2015)
;

select code,amount,month,year
from @Table1 s
unpivot
(
  amount
  for val in (salary, TA, DA)
) u;