如何将我的colums数据转换为sql server中的行

时间:2015-05-28 09:27:28

标签: sql-server-2008-r2

我正在使用sql server中的在线考试数据库,其中我有一个具有以下结构的表: -

id question option1 option2 option3 option4 option1_image option2_image option3_image option4_image

我想要以下格式的数据

id  options  option_image

在选项列中,我想获取所有选项值,在option_image列中我想要所有图像。我正在使用以下查询

select  id,Options,Option_Image
from Questions
unpivot
(
  Options
  for col in (option1, option2,option3,option4)

) u
unpivot
(
Option_Image
for img in(option1_image,option2_image,option3_image,option4_image)
) v

但它没有工作

1 个答案:

答案 0 :(得分:0)

我不知道这对于大量的记录是否有益 但你可以尝试这个

select ID,option1,option1_img from Questions
union all
select ID,option2,option2_img from Questions
union all
select ID,option3,option3_img from Questions
union all
select ID,option4,option4_img from Questions
相关问题