这篇文章主要为大家详细介绍了SQL Server sql多表行转列、级联行转列功能实例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
现有两表A,B代码如下:
declare @sql varchar(2000)
set @sql='select CONVERT(varchar(100), a.[Time], 23) as 时间,count(b.title) as 总数'
select @sql =@sql+ ',sum(case b.title when '''+Title+''' then 1 else 0 end) as '+'['+Title+']'
from (select distinct title from B) as B
set @sql=@sql+' from A as a left join B as b on a.AId=B.Aid
group by CONVERT(varchar(100), a.[Time], 23) order by CONVERT(varchar(100), a.[Time], 23) desc'
exec(@sql)
时间 | 总数 | 商品1 | 商品2 | 商品3 |
2013-11-11 | 5 | 1 | 0 | 4 |
2013-11-10 | 13 | 2 | 11 | 1 |
本文来自:http://www.q1010.com/179/8251-0.html
注:关于SQL Server sql多表行转列、级联行转列功能实例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:SQL SERVER
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。