mssql

推荐列表 站点导航

当前位置:首页 > 数据库 > mssql >

sql动态行转列的两种方法

来源:网络整理  作者:网络  发布时间:2020-12-12 18:08
sql动态行转列的两种方法,需要的朋友可以参考一下...

select *from ( select Url,case when Month=01 then '1月' when Month=02 then '2月' when Month=03 then '3月' when Month=04 then '4月' when Month=05 then '5月' when Month=06 then '6月' when Month=07 then '7月' when Month=08 then '8月' when Month=09 then '9月' when Month=10 then ' 10月' when Month=11 then '11月' when Month=12 then ' 12月'

group by CONVERT(varchar(7),AuditingTime,20) print @sql declare @sql2 varchar(8000)='' set @sql2=' select *from (

end month,Quality from (

) as hh pivot (sum(Quality) for AuditingTime in (' + @sql + ')) b'

group by Url, CONVERT(varchar(7),AuditingTime,20)

复制代码 代码如下:

第二种方法:


print @sql2

复制代码 代码如下:


pivot ( sum(Quality) for month in([1月],[2月],[3月],[4月],[5月],[6月],[7月],[8月],[9月],[10月],[11月],[12月])) as a

exec(@sql2)

select Url, CONVERT(varchar(7),AuditingTime,20) AuditingTime,SUM(Quality) Quality from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where b.ID>0 and Auditing=2

select @sql = isnull(@sql + ',' , '') + '['+CONVERT(varchar(7),AuditingTime,20)+']'

declare @sql varchar(8000)

group by Url,DATENAME(M,AuditingTime) )as h ) as hh

select Url,DATENAME(M,AuditingTime)Month,SUM(Quality) Quality from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where AuditingTime>'2013-01-01' and b.ID>0 and Auditing=2

from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where AuditingTime>'2013-01-01' and b.ID>0 and Auditing=2

相关热词: 方法

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://www.juheyunku.com/sql/mssql/3014.shtml

最新文章
sql server 关于设置null的一 sql server 关于设置null的一

时间:2020-12-28

详解SQL游标的用法 详解SQL游标的用法

时间:2020-12-27

vs code连接sql server数据库步 vs code连接sql server数据库步

时间:2020-12-27

图书管理系统的sqlserver数 图书管理系统的sqlserver数

时间:2020-12-25

详解SQL 通配符 详解SQL 通配符

时间:2020-12-25

sql四大排名函数之ROW_NUM sql四大排名函数之ROW_NUM

时间:2020-12-25

SQLServer数据库处于恢复挂 SQLServer数据库处于恢复挂

时间:2020-12-24

Win10 64位安装个人版SQL20 Win10 64位安装个人版SQL20

时间:2020-12-24

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

sql动态行转列的两种方法

2020-12-12 编辑:网络

select *from ( select Url,case when Month=01 then '1月' when Month=02 then '2月' when Month=03 then '3月' when Month=04 then '4月' when Month=05 then '5月' when Month=06 then '6月' when Month=07 then '7月' when Month=08 then '8月' when Month=09 then '9月' when Month=10 then ' 10月' when Month=11 then '11月' when Month=12 then ' 12月'

group by CONVERT(varchar(7),AuditingTime,20) print @sql declare @sql2 varchar(8000)='' set @sql2=' select *from (

end month,Quality from (

) as hh pivot (sum(Quality) for AuditingTime in (' + @sql + ')) b'

group by Url, CONVERT(varchar(7),AuditingTime,20)

复制代码 代码如下:

第二种方法:


print @sql2

复制代码 代码如下:


pivot ( sum(Quality) for month in([1月],[2月],[3月],[4月],[5月],[6月],[7月],[8月],[9月],[10月],[11月],[12月])) as a

exec(@sql2)

select Url, CONVERT(varchar(7),AuditingTime,20) AuditingTime,SUM(Quality) Quality from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where b.ID>0 and Auditing=2

select @sql = isnull(@sql + ',' , '') + '['+CONVERT(varchar(7),AuditingTime,20)+']'

declare @sql varchar(8000)

group by Url,DATENAME(M,AuditingTime) )as h ) as hh

select Url,DATENAME(M,AuditingTime)Month,SUM(Quality) Quality from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where AuditingTime>'2013-01-01' and b.ID>0 and Auditing=2

from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where AuditingTime>'2013-01-01' and b.ID>0 and Auditing=2

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://www.juheyunku.com/sql/mssql/3014.shtml

相关文章

风云图片

推荐阅读

返回mssql频道首页