求select? 横向数据到纵向数据的转换

Vicart 2004-01-13 03:24:08

感谢您查看此贴!

有如下表(FieldA, FieldB,FieldC为int)
ID FieldA FieldB FieldC
1 10 15 12
2 11 12 14
希望通过存储过程返回

ID Field
1 10
1 15
1 12
2 11
2 12
2 14

多谢!
...全文
48 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
txlicenhe 2004-01-13
  • 打赏
  • 举报
回复
--测试:
create table t1(ID int,FieldA int,FieldB int,FieldC int)
insert into t1 select '1','10','15','12'
insert into t1 select '2','11','12','14'
go
select id,fielda from (
select id,fieldA,1 as flag from t1
union all
select id,fieldB,2 from t1
union all
select id,fieldc,3 from t1
) aa order by id,flag

/*
id fielda
----------- -----------
1 10
1 15
1 12
2 11
2 12
2 14

(所影响的行数为 6 行)

*/

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧