因此,我具有以下表结构:
ID Autor1 Autor2 Autor3 Redactor1 Redactor2 CategorieDeVarsta1
--------------------------------------------------------------------------------
1 Ursula K. Le Guin a c a c 14-15 ani
2 Natalie Babbitt b d b d 12-13 ani
我希望所有列标题名称(Autor1,Autor2等)都放在一列下,假设Code
,而每一列的值都放在第二列下。这可能吗?我尝试使用PIVOT
和CROSS APPLY
却无法做到。有人可以给我一个提示吗?谢谢
答案 0 :(得分:1)
想要取消枢纽吗?
Select ID, Code, Val
From (Select id, Autor1, Autor2, Autor3, Redactor1, Redactor2, CategorieDeVarstal From autor) p
UnPivot
(Val FOR Code IN (Autor1, Autor2, Autor3, Redactor1, Redactor2, CategorieDeVarstal)
) AS unpvt
结果:
ID Code Val
1 Autor1 Ursula K. Le Guin
1 Autor2 a
1 Autor3 c
1 Redactor1 a
1 Redactor2 c
1 CategorieDeVarstal 14-15 ani
2 Autor1 Natalie Babbitt
2 Autor2 b
2 Autor3 d
2 Redactor1 b
2 Redactor2 d
2 CategorieDeVarstal 12-13 ani