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月'
end month,Quality from (
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
group by Url,DATENAME(M,AuditingTime) )as h ) as hh
pivot ( sum(Quality) for month in([1月],[2月],[3月],[4月],[5月],[6月],[7月],[8月],[9月],[10月],[11月],[12月])) as a
第二种方法:
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + '['+CONVERT(varchar(7),AuditingTime,20)+']'
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
group by CONVERT(varchar(7),AuditingTime,20) print @sql declare @sql2 varchar(8000)='' set @sql2=' select *from (
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
group by Url, CONVERT(varchar(7),AuditingTime,20)
) as hh pivot (sum(Quality) for AuditingTime in (' + @sql + ')) b'
print @sql2
exec(@sql2)
相关文章
卸载VS2011 Developer Preview后Sql Server2008 R2建立数据库关系图报“找不到指定的
话说上回我为了尝尝螃蟹的味道而装了 VS2011 Developer Preview,但是装完后立马卸载掉了,原因是这家伙的安装目录位置没用,我设置到D盘的但是装完后D盘的文件夹只有一百多M,而足足8G+的空间是在C盘上消耗的。2011-11-11SQL Server误区30日谈 第11天 镜像在检测到故障后瞬间就能故障转移
数据库镜像的故障转移既可以自动发起,也可以手动发起2013-01-01在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名
在项目开发中如果有时修改了一个存储过程,但是如何能够快速的查找到使用了这个存储过程的其它存储过程呢2012-06-06揭秘SQL Server 2014有哪些新特性(3)-可更新列存储聚集索引
可更新的列存储索引作为SQL Server 2014的一个关键功能之一,在提升数据库的查询性能方面贡献非常突出。据微软统计,在面向OLAP查询统计类系统中,相比其他SQL传统版本的数据库,报表查询的性能最大可提升上十倍。2014-08-08
最新评论