广告
返回顶部
首页 > 资讯 > 数据库 >一个简单的交叉报表_列转行
  • 948
分享到

一个简单的交叉报表_列转行

一个简单的交叉报表_列转行 2021-10-29 20:10:28 948人浏览 猪猪侠
摘要

--列转行小实例 --创建测试表 if object_id(N"test", N"U") is not null drop table test Go with UnPivotTable as ( select

一个简单的交叉报表_列转行

--列转行小实例
--创建测试
if object_id(N"test", N"U") is not null
  drop table test
Go
with UnPivotTable as
(
  select 1 as UserNO, "33" as A, "44" AS B, "55" as C
  uNIOn all
  select 2 as UserNO, "23" as A, "34" AS B, "56" as C
)
select * into test from UnPivotTable
go
--创建存储过程
if exists(select name from sysobjects where name = "usp_GetUnPivotInfo")
    drop proc usp_GetUnPivotInfo
go

create proc usp_GetUnPivotInfo
as
declare @sql nvarchar(4000)        
SELECT @SQL=isnull(@SQL+",","")+quotename(Name) FROM syscolumns
WHERE ID=object_id("test") and [name] not in ("UserNO") ORDER BY Colid
SET @SQL="select UserNO,[Attr],[value] from (select * from test) a unpivot ([value] for [Attr] in("+@SQL+"))b"
exec(@SQL);
go

exec usp_GetUnPivotInfo ;

交叉前

交叉后

 

您可能感兴趣的文档:

--结束END--

本文标题: 一个简单的交叉报表_列转行

本文链接: https://www.lsjlt.com/news/6136.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作