sqlserver 行转列
发布时间:2020-12-12 13:00:03 所属栏目:MsSql教程 来源:网络整理
导读:还写了一篇 Linq 实现 DataTable 行转列 有时间大家可以看一下 sqlserver把行转成列在我们编码中是经常遇到的我做一个小例子大家看一下 1 -- 创建一个表 2 create table PayPhoneMoney 3 ( 4 id int identity ( 1 , 1 ), 5 userName Nvarchar ( 20 ),128); l
还写了一篇Linq 实现 DataTable 行转列有时间大家可以看一下 sqlserver把行转成列在我们编码中是经常遇到的我做一个小例子大家看一下 1 --创建一个表 看一下表中的数据 我们要想查一下每个人所有支付形式下的总钱数如图所示 3 select userName,128); line-height:1.5!important">4 sum(case payType when ' then money else 0 end) as 支付宝,128); line-height:1.5!important">5 as 工行,128); line-height:1.5!important">6 as 交行,128); line-height:1.5!important">7 as 中行 8 from PayPhoneMoney 9 by userName --我们这只列出了几种支付方式实际中还有很多支付方式不能一个一个都用case when 吧 set @cmdText=select userName,' select =+ sum(case payType when'''+payType'''Then money else 0 end) as +payType 4 ,0); line-height:1.5!important">'+char(10) from (Distinct payType from PayPhoneMoney) T 5 print @cmdText发现多一个逗号下面把逗号去掉6 =left(@cmdText,255); line-height:1.5!important">len(@cmdText)-2)去掉逗号7 from PayPhoneMoney group by userName@cmdText exec(@cmdText) 看一下结果是一样的吧 ?转载:http://www.cnblogs.com/li-peng/archive/2012/02/01/2334973.html 作者: 李鹏 出处: http://www.cnblogs.com/li-peng/(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |