我有这样的表
id | vname1 | vname2 | vname3
1 | vala | valb | valc
我想让它像这样转换
id | vname | vals
1 | vname1 | vala
1 | vname2 | valb
1 | vname3 | valc
我考虑过旋转,但在这里我认为不是这样的
答案 0 :(得分:1)
为每个vname列执行UNION ALL
,其中一个SELECT
:
select id, 'vname1' as vname, vname1 as vals from tablename
union all
select id, 'vname2' as vname, vname2 as vals from tablename
union all
select id, 'vname3' as vname, vname3 as vals from tablename
答案 1 :(得分:1)
您可以使用 UNPIVOT 功能将列转换为行:
示例示例:
select Id,
indicatorname,
from yourtable
unpivot
(
indicatorvalue
for indicatorname in (Indicator1, Indicator2, Indicator3)
) unpiv;
参考链接:UnPivot