答案 0 :(得分:2)
使用apply
:
select t.collegeid, t.deptid, t.empid, v.yr, v.mnth, v.act, v.pred
from t outer apply
(values (act201701, pred201701, 2017, 1),
(act201702, pred201702, 2017, 2),
(act201703, pred201703, 2017, 3),
) v(act, pred, yr, mnth);
你也可以使用unpivot
做同样的事情。但是,apply
实现了横向连接,这比仅仅解开数据更强大。
答案 1 :(得分:1)
如果列名是静态的,您可能可以使用UNION SELECT查询,如下所示:
Select CollegeID, DeptID, EmpID, "2017" As Y, "1" As Mnth, [Act201701] As Act, [Pred201701] As Pred from [SomeTable]
UNION
Select CollegeID, DeptID, EmpID, "2017" As Y, "2" As Mnth, [Act201702] As Act, [Pred201702] As Pred from [SomeTable]
UNION
Select CollegeID, DeptID, EmpID, "2017" As Y, "3" As Mnth, [Act201703] As Act, [Pred201703] As Pred from [SomeTable]
其中SomeTable
是您的表名。
答案 2 :(得分:0)
替换表名并尝试
insert into yourTableName (CollegeID, DeptID, EmpID, Yr, Mnth, Act, Pred) values (234, 34, 4, 2017, 1, 6131.86, 6131.82)