SQL 列转行问题

zaonian 2012-05-14 03:30:56
时段 访问量 受理量 放弃量
1 1 1 3
2 4 5 6
3 6 4 5

要转换为:
时段 1 2 3
访问量 1 4 6
受理量 1 5 4
这种形式的....求SQL语句及简单注释呀...
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([时段] int,[访问量] int,[受理量] int,[放弃量] int)
insert [test]
select 1,1,1,3 union all
select 2,4,5,6 union all
select 3,6,4,5
select '访问量' as 时间,
max(case when [时段]=1 then [访问量] else 0 end) as [1],
max(case when [时段]=2 then [访问量] else 0 end) as [2],
max(case when [时段]=3 then [访问量] else 0 end) as [3]
from test
union all
select '受理量' as 时间,
max(case when [时段]=1 then 受理量 else 0 end) as [1],
max(case when [时段]=2 then 受理量 else 0 end) as [2],
max(case when [时段]=3 then 受理量 else 0 end) as [3]
from test
union all
select '放弃量' as 时间,
max(case when [时段]=1 then 放弃量 else 0 end) as [1],
max(case when [时段]=2 then 放弃量 else 0 end) as [2],
max(case when [时段]=3 then 放弃量 else 0 end) as [3]
from test

/*
时间 1 2 3
访问量 1 4 6
受理量 1 5 4
放弃量 3 6 5
*/

简单易懂
--小F-- 2012-05-14
  • 打赏
  • 举报
回复
90°旋转 见精华帖子。

22,209

社区成员

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

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